dispatch_async and calling a completion handler on the original queue

后端 未结 4 1750
忘掉有多难
忘掉有多难 2020-12-08 10:26

I\'ve seen some related questions but none seem to answer this case. I want to write a method that will do some work in the background. I need this method to call a completi

4条回答
  •  时光说笑
    2020-12-08 11:11

    not sure if this will solve the problem, but how about using NSOperations instead of GCD?:

    - (void)someMethod:(void (^)(BOOL result))completionHandler {
    NSOperationQueue *current_queue = [NSOperationQueue currentQueue];
    
    // some setup code here
    NSOperationQueue *q = [[NSOperationQueue alloc] init];
    [q addOperationWithBlock:^{
        BOOL ok = YES;// some result
    
        // do some long running processing here
        [current_queue addOperationWithBlock:^{
            completionHandler(ok);
        }];
    }];
    

提交回复
热议问题