等到多个网络请求都执行完毕

编程入门 行业动态 更新时间:2024-10-20 03:51:15
本文介绍了等到多个网络请求都执行完毕 - 包括它们的完成块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有多个操作(它们是 AFNetworking 请求),其中包含需要一些时间来执行的完成块,以及需要在所有请求结束时保存的核心数据对象.

I have multiple operations (they're AFNetworking requests) with completion blocks that takes some time to execute, and a Core Data object that needs to be saved at the end of all the requests.

MyCoreDataObject *coreDataObject; AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:request1]; [operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { coreDataObject.attribute1 = responseObject; sleep(5); }]; [operation1 start]; AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request1]; [operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { coreDataObject.attribute2 = responseObject; sleep(10); }]; [operation1 operation2]; [context save:nil];

当然,这不会如我所愿,因为请求是异步的.我尝试添加一个 NSOperationQueue 像这样:

Of course, this does not work as I want because the requests are asynchronous. I tried adding an NSOperationQueue like so:

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; [operationQueue setMaxConcurrentOperationCount:2]; AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:request1]; [operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { coreDataObject.attribute1 = responseObject; sleep(5); }]; [operationQueue addOperation:operation1]; AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request1]; [operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { coreDataObject.attribute2 = responseObject; sleep(10); }]; [operationQueue addOperation:operation2]; [imageQueue waitUntilAllOperationsAreFinished]; [context save:nil];

这看起来好一点.使用 waitUntilAllOperationsAreFinished,我的队列阻塞当前线程,直到我的请求完成,但直到我的成功块完成,这才是我真正需要的.

This looks a bit better. Using waitUntilAllOperationsAreFinished, my queue blocks the current thread until my requests are finished, but not until my success Blocks are finished, which is really what I need.

关于如何以好的方式实现这一目标的任何想法?

Any ideas on how to achieve this in a good way?

推荐答案

使用调度组.

dispatch_group_t group = dispatch_group_create(); MyCoreDataObject *coreDataObject; dispatch_group_enter(group); AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:request1]; [operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { coreDataObject.attribute1 = responseObject; sleep(5); dispatch_group_leave(group); }]; [operation1 start]; dispatch_group_enter(group); AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request1]; [operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { coreDataObject.attribute2 = responseObject; sleep(10); dispatch_group_leave(group); }]; [operation2 start]; dispatch_group_wait(group, DISPATCH_TIME_FOREVER); dispatch_release(group); [context save:nil];

更多推荐

等到多个网络请求都执行完毕

本文发布于:2023-11-26 06:53:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633087.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   网络

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!