AFNetworking enqueRequestOperations在调用所有AFJSONRequestOperation完成之前触发完成

时间:2013-04-18 14:42:39

标签: ios afnetworking

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://twitter.com"]];

NSURL *url = [NSURL URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"];
NSURLRequest *r = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:r success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"App.net Global Stream: %@", JSON);
} failure:nil];
AFJSONRequestOperation *operation1 = [AFJSONRequestOperation JSONRequestOperationWithRequest:r success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"App.net Global Stream: %@", JSON);
} failure:nil];

[client enqueueBatchOfHTTPRequestOperations:@[operation, operation1] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {

} completionBlock:^(NSArray *operations) {
    NSLog(@"%@", operations);
}];

enqueueBatchOfHTTPRequestOperations在操作和operation1阻塞之前触发。

已阅读https://github.com/AFNetworking/AFNetworking/issues/362并尝试使用dispatch_group进行可能的修复,但仍然无效。

1 个答案:

答案 0 :(得分:1)

更改您的代码以使用AFHTTPRequestOperation而不是AFJSONRequestOperation,并使用NSJSONSerialization手动解析JSON,您会发现在完成所有操作后,您的队列完成块将被触发。