I want to package AFNetworking in order to that urls can request one by one , but o don't know how to package it. for example , there are three url request want to send to server , I hope that send one url request by AFNetworking first , and when the first one has finished , then send next one to server, and so on 。 I think it may need NSOperationQueue to do this job, but I don't know how to implementation it. Is anyone has write code like this? could you give me the .h and .m file to me ,so that I can modeling writing or use direct ,thanks a lot.
You probably want something like this if you are sending data like images or whatnot. If you are just sending simple requests then just use the defacto AFNetworking GET/POST instead of multipartFormRequestWithMethod
.
In short, create an operation for each request and add each one to an array. Then use batchOfRequestOperations
to perform each one. (From the docs).
NSMutableArray *mutableOperations = [NSMutableArray array];
for (NSURL *fileURL in filesToUpload) {
NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[mutableOperations addObject:operation];
}
NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:mutableOperations 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];