Waiting for an asyncronous call to finish within a completion block

前端 未结 1 763
眼角桃花
眼角桃花 2021-01-27 17:47

I am currently working on a POC app, I have previously posted about it here. I am trying to handle automatic refreshing of an authentication token should my server give me a 401

相关标签:
1条回答
  • 2021-01-27 18:44

    Assuming you can change implementation of requestAuth, add a completion handler parameter to requestAuth function.
    Inside requestAuth implementation, call that handler after token is received. Then in requestDataForUser:

    case 401:
         NSLog(@"401 Challenge - Retrying Authentication, Attempt %ld", (long)retryAttempts);
         [weakSelf refreshAuth withCompletionHandler:weakself.requestDataForUser];
         retryAttempts += 1;
         break;
    

    Otherwise, use NSOperationQueue and set maximum concurrent operation to 1:

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    queue.maxConcurrentOperationCount = 1;
    
    [queue addOperationWithBlock:^{
        [weakself requestAuth];
    }];
    
    [queue addOperationWithBlock:^{
        [weakself requestDataForUser];
    }];
    
    0 讨论(0)
提交回复
热议问题