Wait until all iOS blocks are executed before moving on

后端 未结 1 1478
迷失自我
迷失自我 2021-01-25 17:21

I have a data model/store object that interfaces with the Internet over several APIs containing data. The number of APIs to be interfaced with is dynamic: from a conceptual stan

相关标签:
1条回答
  • 2021-01-25 17:26

    I played around with dispatch groups the other day, here is a really helpful blog post by jrturton which should help you with the basics!

    But basically it looks like your missing the line to enter/leave the dispatch group. So non of your runAPI methods are being run as you have no items in the group and [[NSNotificationCenter defaultCenter] postNotificationName:@"apiDataUpdated" object:self]; gets called straight away.

    dispatch_group_t group = dispatch_group_create();
    
    for(MyAPIEndpoint __weak __block *ep in apiList)
    {
        dispatch_group_enter(group);
        [self runAPI:ep withCompletionBlock:^(MyAPIEndpoint *api, NSError *err)
        {
            // update the data model here, code omitted as it's not relevant
            dispatch_group_leave(group);
        }];
    }
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(),^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"apiDataUpdated" object:self];
    });
    
    0 讨论(0)
提交回复
热议问题