gpt4 book ai didi

ios - 如何更正 block 函数 ObjC 中的调度主队列任务?

转载 作者:行者123 更新时间:2023-11-28 19:29:03 25 4
gpt4 key购买 nike

正在从服务器加载一些数据,但循环(for .. in ..)在实际加载数据之前完成。如何更正主队列上的实现调度或其他错误?

[ApiManager getCategoriesifSuccess:^{
[self.sectionArray addObjectsFromArray:[CategoryManager getCats]];
for (CategorySectionModel *mod in [CategoryManager getCats]) {
[self.arrayForBool addObject:@"YES"];
[ApiManager getCatalogItemsInCity:currentCity withSection:mod.uid
andStart:@"1" andLimit:@"20" ifSuccess:^{
[self.itemsArray setObject:[CatalogItemManager getItems] forKey:mod.uid];
[indicator stopAnimating];
[self.tableView reloadData];
NSLog(@"DONE WITH SECTION:%@", mod.title);
} orIfFailed:^(NSString *fail) {
NSLog(@"%@", fail);
}];
NSLog(@"LOOP");
};
} orIfFailed:^{
}];

控制台返回这个,但它似乎不正确:

2017-12-24 01:17:57.148355+0300 iOS[75679:1805296] LOOP
2017-12-24 01:17:57.149576+0300 iOS[75679:1805296] LOOP
2017-12-24 01:17:57.150786+0300 iOS[75679:1805296] LOOP
2017-12-24 01:17:57.152773+0300 iOS[75679:1805296] LOOP
2017-12-24 01:17:57.154727+0300 iOS[75679:1805296] LOOP
2017-12-24 01:17:57.156987+0300 iOS[75679:1805296] LOOP
2017-12-24 01:17:57.158840+0300 iOS[75679:1805296] LOOP
2017-12-24 01:17:57.981119+0300 iOS[75679:1805296] DONE WITH SECTION:First
2017-12-24 01:17:58.285569+0300 iOS[75679:1805296] DONE WITH SECTION:Second
2017-12-24 01:17:58.403725+0300 iOS[75679:1805296] DONE WITH SECTION:Third
2017-12-24 01:17:58.434170+0300 iOS[75679:1805296] DONE WITH SECTION:Fourth
2017-12-24 01:17:58.449970+0300 iOS[75679:1805296] DONE WITH SECTION:Fifth
2017-12-24 01:17:58.469519+0300 iOS[75679:1805296] DONE WITH SECTION:Sixth
2017-12-24 01:17:58.535116+0300 iOS[75679:1805296] DONE WITH SECTION:Seventh

最佳答案

使用调度组:

Swift(伪代码):

    let group = DispatchGroup()

for ... {
group.enter()

someAsyncMethod({
group.leave()
})
}
group.notify(queue: DispatchQueue.main, execute: {
// you code here will be invoked after all group.leave()
})

确保为每个 group.enter() 只调用一个 (!) group.leave()。如果有失败回调,那么成功和失败回调必须调用group.leave()

关于ios - 如何更正 block 函数 ObjC 中的调度主队列任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47956485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com