NSOperation wait until asynchronous block executes

前端 未结 2 1480
滥情空心
滥情空心 2020-12-14 19:25

i need to put asynchronous operations into an operation queue, however, they need to execute on after the other

self.operationQueue = [NSOperationQueue new];         


        
2条回答
  •  一向
    一向 (楼主)
    2020-12-14 19:45

    What I did was playing with [myQueue setSuspended:YES] and [myQueue setSuspended:NO] before and after respectively.

    For example:

    [myQueue addOperationWithBlock:^{
        [myQueue setSuspended:YES];
        [someBackendService doSomeAsyncJobWithCompletionBlock:^{
            callback(YES, nil);
            [myQueue setSuspended:NO];
        });
    }];
    

    The achieved effect is that the queue is suspended before the async task, therefore even though the operation block is returned, it only starts the next operation (subject to maxConcurrentOperationCount of course) when the async task's completion block is called.

提交回复
热议问题