顺序执行
LastStoriesModel* lastStoriesModel = [_lastStoriesModelArray lastObject];
NSString* lastDate1 = lastStoriesModel.date;
NSString* lastDate2 = [DateTool dateMinusOneWhithTimeString:lastDate1];
NSString* lastDate3 = [DateTool dateMinusOneWhithTimeString:lastDate2];
[_manage getLastTime:lastDate1 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
[self->_lastStoriesModelArray addObject:lastStoriesModel];
[self sendStoriserToView:lastStoriesModel];
[self->_manage getLastTime:lastDate2 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
[self->_lastStoriesModelArray addObject:lastStoriesModel];
[self sendStoriserToView:lastStoriesModel];
[self->_manage getLastTime:lastDate3 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
[self->_lastStoriesModelArray addObject:lastStoriesModel];
[self sendStoriserToView:lastStoriesModel];
dispatch_async(dispatch_get_main_queue(), ^{
[self->_interFaceView viewInit];
});
} error:^(NSError * _Nonnull error) {
NSLog(@"getLastModel error");
}];
} error:^(NSError * _Nonnull error) {
NSLog(@"getLastModel error");
}];
} error:^(NSError * _Nonnull error) {
NSLog(@"getLastModel error");
}];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
[self->_manage getLastTime:lastDate3 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
[self->_lastStoriesModelArray addObject:lastStoriesModel];
[self sendStoriserToView:lastStoriesModel];
dispatch_async(dispatch_get_main_queue(), ^{
[self->_interFaceView viewInit];
NSLog(@"任务一完成");
});
} error:^(NSError * _Nonnull error) {
NSLog(@"getLastModel error");
}];
});
dispatch_group_async(group, queue, ^{
...
NSLog(@"任务二完成");
});
dispatch_group_async(group, queue, ^{
...
NSLog(@"任务三完成");
});
//在分组的所有任务完成后触发
dispatch_group_notify(group, queue, ^{
...
NSLog(@"所有任务完成");
});
-(void)serialByGroupWait {
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[self->_manage getLastTime:lastDate1 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
dispatch_group_leave(group);
[self->_lastStoriesModelArray addObject:lastStoriesModel];
[self sendStoriserToView:lastStoriesModel];
dispatch_async(dispatch_get_main_queue(), ^{
[self->_interFaceView viewInit];
NSLog(@"任务一完成");
});
} error:^(NSError * _Nonnull error) {
dispatch_group_leave(group);
NSLog(@"getLastModel error");
}];
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);// 1执行完 下面才会执行
dispatch_group_enter(group);
[self->_manage getLastTime:lastDate2 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
dispatch_group_leave(group);
[self->_lastStoriesModelArray addObject:lastStoriesModel];
[self sendStoriserToView:lastStoriesModel];
dispatch_async(dispatch_get_main_queue(), ^{
[self->_interFaceView viewInit];
NSLog(@"任务二完成");
});
} error:^(NSError * _Nonnull error) {
dispatch_group_leave(group);
NSLog(@"getLastModel error");
}];
// 1 2都完成 才会执行
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
NSLog(@"任务完成");
});
}
循环请求
我是用dispatch_group_enter/leave/wait控制请求顺序。
dispatch_group_t group = dispatch_group_create();
for (NSString* ID in self.myCollectModel.collectSet) {
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
dispatch_group_enter(group);
[[Manage sharedManage] getCollectConentWithID:ID CollectData:^(MyCollectContentModel * _Nonnull myCollectContenModel) {
[self.myCollectModel.collectTitle addObject:myCollectContenModel.title];
[self.myCollectModel.collectImagePath addObject:myCollectContenModel.image];
dispatch_group_leave(group);
} error:^(NSError * _Nonnull error) {
NSLog(@"get Collect error");
dispatch_group_leave(group);
}];
}
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
self.myCollectView.titleArray = self.myCollectModel.collectTitle;
self.myCollectView.imagePathArray = self.myCollectModel.collectImagePath;
[self.myCollectView viewReload];
});
});
原文地址:https://blog.csdn.net/m0_63852285/article/details/127972883
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_29292.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。