2013-05-27 37 views
0

我的应用程序从Flickr上的用户下载大量信息和图像,并使用AFHttpClient。所以基本上,每个人都会异步执行实际上是NSUrlConnection,然后运行完成块。一个类对每个api调用都有方法,另一个类通过api调用循环来获取每个用户的数据,并将其放入核心数据。然而,我的问题是,我无法找到调用api类的类来确定何时完成所有下载并将其放入核心数据的方法。另外,我将同时下载多个用户数据,并根据用户的不同,完成不同顺序的加载。有什么建议么?检查多个异步网络操作何时完成

+0

是你能找到任何解决办法?请张贴一些。 – Meet

回答

0

使用实例变量记录正在进行的活动请求的数量。这可能是一个简单的NSInteger,它可以递增或递减,或者它可以是一个字典,用户名键入,如果需要,可以保存一些或更多详细的跟踪记录。这真的取决于你需要的细节水平。使API调用的类应该完成对这个记录数据的所有管理,并提供'count'方法。

1

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 

}];