Wait until multiple networking requests have all executed - including their completion blocks

后端 未结 4 1140
攒了一身酷
攒了一身酷 2020-11-27 10:33

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

4条回答
  •  情书的邮戳
    2020-11-27 10:42

    I belive something like this:

    NSMutableArray *mutableOperations = [NSMutableArray array];
    for (NSURL *fileURL in filesToUpload) {
        NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id formData) {
            [formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];
        }];
    
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    
        [mutableOperations addObject:operation];
    }
    
    NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:@[...] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
        NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations);
    } completionBlock:^(NSArray *operations) {
        NSLog(@"All operations in batch complete");
    }];
    [[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];
    

    refering to docs: http://cocoadocs.org/docsets/AFNetworking/2.5.0/

提交回复
热议问题