且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

等到所有iOS区块执行完毕后再继续

更新时间:2023-11-20 23:28:10

前几天,我和调度小组一起玩,这对 jrturton 应该可以帮助您了解基础知识!

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!

但是基本上,您似乎缺少进入/离开调度组的行.因此,您的runAPI方法中的任何一个都没有运行,因为您的组中没有项目,并且[[NSNotificationCenter defaultCenter] postNotificationName:@"apiDataUpdated" object:self];被立即调用.

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];
});