gpt4 book ai didi

ios - 检查多个异步网络操作何时完成

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

我的应用程序从 Flickr 上的用户那里下载大量信息和图像,并且我使用 AFHttpClient。所以基本上,每个人都会异步地执行有效的 NSUrlConnection 操作,然后运行完成 block 。一个类为每个 api 调用提供方法,另一个类循环执行 api 调用以获取每个用户数据,并将其放入核心数据中。但是,我的问题是,我无法找到一种方法让调用 api 类的类确定所有下载何时完成并将其放入核心数据中。另外,我将同时下载多个用户数据,并且不同的属性根据用户以不同的顺序完成加载。有什么建议吗?

最佳答案

AFNetworking 使管理多个请求和最终回调变得非常简单,只需使用:

- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations 
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
completionBlock:(void (^)(NSArray *operations))completionBlock;

- (void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)urlRequests
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
completionBlock:(void (^)(NSArray *operations))completionBlock;

在 AFHHTPClient 实例上。

示例:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]];
NSURLRequest *otherRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]];

AFHTTPRequestOperation *operationForImages = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operationForImages setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

//success of images request
self.imageDictionary = responseObject;

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

//manage error for this request

}];
AFHTTPRequestOperation *operationForText = [[AFHTTPRequestOperation alloc] initWithRequest:otherRequest];
[operationForText setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

//success of text request
self.textDictionary = responseObject;

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

//manage error for this request

}];


[[MyPersonalAFHTTPClient sharedClient] enqueueBatchOfHTTPRequestOperations:@[operationForImages,operationForText] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {

//track progression of requests

} completionBlock:^(NSArray *operations) {

//all the request are completed

}];

关于ios - 检查多个异步网络操作何时完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16765103/

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