iOS GCD Sync with Async Block

后端 未结 2 1782
情深已故
情深已故 2021-01-06 14:35

I have an async function with a block :

[self performAsyncTaskCompletion:(void(^) () )
 {
   //Do Something
 }
];

I need to call this funct

2条回答
  •  忘掉有多难
    2021-01-06 15:37

    You could use dispatch group if you want to initiate some process upon the completion of a series of asynchronous tasks, but would like to allow those tasks to run concurrently with respect to each other (which, especially with network requests, can offer much better performance than running them sequentially):

    dispatch_group_t group = dispatch_group_create();
    
    for (int i = 0; i < array.count; i++) {
        dispatch_group_enter(group);
        [self performAsyncTaskCompletion: ^{
            //Do Something
            dispatch_group_leave(group);
        }];
    }
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        // do this when its all done
    });
    

    Personally, I'd might even be inclined to perform a more radical refactoring of performAsyncTaskCompletion, using an asynchronous NSOperation subclass pattern instead. Then you could add these to a NSOperationQueue with maxConcurrentOperationCount specified, thereby achieving the same concurrency while also controlling the degree of concurrency. But hopefully the above illustrates the idea: Run tasks concurrently, but detect the completion of those tasks without ever blocking the main thread.

提交回复
热议问题