iOS GCD Sync with Async Block

為{幸葍}努か 提交于 2019-11-30 23:07:55

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.

ninjaproger

You can use dispatch_async with semaphores:

Example:

- (void)performAsyncTaskCompletion:(void (^)())block {
    if (block) {
        block();
    }
}

- (void)runChainOfOperations {
    static dispatch_once_t onceToken;
    static dispatch_semaphore_t semaphore;
    static dispatch_queue_t queue;

    dispatch_once(&onceToken, ^{
        semaphore = dispatch_semaphore_create(1);
        queue = dispatch_queue_create("com.example.MyApp", NULL);
    });

    NSArray *array = @[@1, @2, @3, @4, @5];

    static long counter = 0;
    for (int i = 0; i < array.count; i++) {
        dispatch_async(queue, ^{
            dispatch_semaphore_wait(semaphore,  DISPATCH_TIME_FOREVER);

            [self performAsyncTaskCompletion:^{
                sleep(10);

                dispatch_async(dispatch_get_main_queue(), ^{
                     NSLog(@"%ld", counter++);
                });

                dispatch_semaphore_signal(semaphore);
             }];  
        });
    }
}

Console output:

2015-04-12 21:28:06.047 HKTest[9497:1136830] 0
2015-04-12 21:28:16.023 HKTest[9497:1136830] 1
2015-04-12 21:28:26.025 HKTest[9497:1136830] 2
2015-04-12 21:28:36.029 HKTest[9497:1136830] 3
2015-04-12 21:28:46.031 HKTest[9497:1136830] 4
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!