In the above, AFHTTPRequestOperationManager is used, and that is usually good enough.
But if you requires batch operations, then you have to use AFHTTPRequestSerializer, AFHTTPRequestOperation and NSOperationQueue.
The sample code below is directly from AFNetworking:
1234567891011121314151617
NSMutableArray*mutableOperations=[NSMutableArrayarray];for(NSURL*fileURLinfilesToUpload){NSURLRequest*request=[[AFHTTPRequestSerializerserializer]multipartFormRequestWithMethod:@"POST"URLString:@"http://example.com/upload"parameters:nilconstructingBodyWithBlock:^(id<AFMultipartFormData>formData){[formDataappendPartWithFileURL:fileURLname:@"images[]"error:nil];}];AFHTTPRequestOperation*operation=[[AFHTTPRequestOperationalloc]initWithRequest:request];[mutableOperationsaddObject:operation];}NSArray*operations=[AFURLConnectionOperationbatchOfRequestOperations:@[...]progressBlock:^(NSUIntegernumberOfFinishedOperations,NSUIntegertotalNumberOfOperations){NSLog(@"%lu of %lu complete",numberOfFinishedOperations,totalNumberOfOperations);}completionBlock:^(NSArray*operations){NSLog(@"All operations in batch complete");}];[[NSOperationQueuemainQueue]addOperations:operationswaitUntilFinished:NO];
More Tips
The above examples use the default AFJSONResponseSerializer – which means it expect the response to be a valid JSON.
// To handle XMLAFHTTPRequestOperationManager*manager=[AFHTTPRequestOperationManagermanager];manager.responseSerializer=[AFXMLParserResponseSerializernew];// Or datamanager.responseSerializer=[AFHTTPResponseSerializernew];
AFNetworking is strict with the Content-Type.
So if your response is a “text/plain” when you are using a JSON serializer, you can do this:
12
// Tip by dStudiosmanager.responseSerializer.acceptableContentTypes=[NSSetsetWithObject:@"text/plain"];