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");
}];
很low,请求一多就没法写。但请求少时还是很实用的。
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];
});
});