How to dispatch_group_wait for dispatch_group_async inside an asynchronous block

前端 未结 1 1954
悲哀的现实
悲哀的现实 2021-01-14 12:52

I have code that looks something like this:

[SVProgressHUD show];
[imageGenerator generateCGImagesAsynchronouslyForTimes:times
                completionHand         


        
相关标签:
1条回答
  • 2021-01-14 13:16

    Think of this method as a static counter available to threads, so when you enter a group the counter increments, and when that block returns, decrements...

    When that counter is 0, it will call a block to invoke

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    
    while(someCondition)
    {
        dispatch_group_enter(group);
       [SomeClassThatLoadsOffTheInternet getMyImages:^{
    
            // do something with these.
            dispatch_group_leave(group);
    
        });
    }
    
    dispatch_group_notify(group, queue, ^{
        // do something when all images have loaded
    });
    

    Is that what you were thinking of?

    0 讨论(0)
提交回复
热议问题