iOS AFNetwork 3.0:是否有一种发送多个API请求并等待所有请求完成的更快方法?
我当前正在使用以下方法发送 GET
API请求。此方法有效,但我想知道是否有更快的方法。关于需求,我需要知道的是何时已同步所有已删除邮件。
I am currently using the following method to send GET
API requests. This method works, but I was wondering if there is a faster way. All I need regarding requirements is to know when all of the Deleted mail has been synced. Any tips or suggestions are appreciated.
- (void)syncDeletedMail:(NSArray *)array atIdx:(NSInteger)idx {
if (idx < array.count) {
NSInteger idNumber = array[idx];
[apiClient deleteMail:idNumber onSuccess:^(id result) {
[self syncDeletedMail:array atIdx:(idx + 1)];
} onFailure:^(NSError *error){
[self syncDeletedMail:array atIdx:(idx + 1)];
}];
} else {
NSLog(@"finished");
}
}
编辑:我不在乎它是什么顺序只要所有API请求都返回完成,就可以完成(不确定速度是否重要)。
I don't care what order it is completed (not sure if it matters in terms of speed), as long as all the API requests come back completed.
您可以立即发送 deleteMail
请求,并使用 dispatch_group
知道所有请求何时完成。下面是实现,
You can just send deleteMail
requests at once and use dispatch_group
to know when all the requests are finished. Below is the implementation,
- (void)syncDeletedMail:(NSArray *)array {
dispatch_group_t serviceGroup = dispatch_group_create();
for (NSInteger* idNumber in array)
{
dispatch_group_enter(serviceGroup);
[apiClient deleteMail:idNumber onSuccess:^(id result) {
dispatch_group_leave(serviceGroup);
} onFailure:^(NSError *error){
dispatch_group_leave(serviceGroup);
}];
}
dispatch_group_notify(serviceGroup,dispatch_get_main_queue(),^{
NSLog(@"All email are deleted!");
});
}
在这里您可以看到所有请求同时被触发,因此将时间从 n
倍减少到 1
。
Here you can see all the requests are fired at the same time so it will reduce the time from n
folds to 1
.