我的应用程序从Flickr上的用户下载了大量信息和图像,我使用了AFHttpClient。所以基本上,每个人都异步地经历了NSUrlConnection
有效的,然后运行完成块。一个类具有针对每个api调用的方法,另一个类通过api调用循环以获取每个用户数据,该数据被放入核心数据中。但是,我的问题是我无法想出一个方法,让类调用api类来确定何时完成所有下载并将其放入核心数据中。此外,我将同时下载多个用户数据,并且不同的属性根据用户的不同顺序完成加载。有什么建议吗?
答案 0 :(得分:1)
AFNetworking使管理多个请求和最终回调变得非常简单,juste使用:
- (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;
例如:
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
}];
答案 1 :(得分:0)
使用实例变量计算正在进行的活动请求数。这可以是一个简单的NSInteger,它可以递增和递减,也可以是一个字典,由用户名键入,并在需要时保存一个或多个详细的跟踪记录。这真的取决于您需要的详细程度。进行API调用的类应该对此记录数据进行所有管理,并且只提供“计数”方法。