我搜索了很多并看到了一些答案,但我仍然不确定如何解决它:我只想在操作完成时执行一些 void 函数。
我的代码:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPBody:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request addValue:kAPP_PASSWORD_VALUE forHTTPHeaderField:kAPP_PASSWORD_HEADER];
[request addValue:@"http://blablabla" forHTTPHeaderField:@"SOAPAction"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
self.responseXML = [operation responseString];
NSData *data = [self.responseXML dataUsingEncoding:NSUTF8StringEncoding];
[self doParse:data];
self.numOfWallPosts = [self.sharedPrefs integerForKey:@"total_number_of_wall_posts"];
for (int i = 1; i <= self.numOfWallPosts; i++)
{
NSString *sharedKey = [NSString stringWithFormat:@"wall_number_%d", i];
NSMutableDictionary *aaa = [[NSMutableDictionary alloc] init];
aaa = [self.sharedPrefs objectForKey:sharedKey];
[self handleWallPostsWithWallPost:aaa];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"\n\nError: %@", error);
}];
[operation start];
[self handleWallPostsWithWallPost:aaa];
- 在后台下载一些图像,因此,该 block 已完成,但在后台,有一些图像正在下载。
我如何知道图像下载何时完成?
- (void)handleWallPostsWithWallPost:(NSMutableDictionary *)wallPost
{
NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];
temp = wallPost;
NSString *owner = [temp objectForKey:@"owner"];
NSString *title = [temp objectForKey:@"title"];
NSString *imageUrl = [temp objectForKey:@"image_url"];
NSInteger wallId = [[temp objectForKey:@"wall_id"] integerValue];
NSLog(@"Wall ID: %d | Owner: %@ | Title: %@ | Image URL: %@", wallId, owner, title, imageUrl);
// arrayOfOwnersImage - Save as NSData
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]];
AFImageRequestOperation *operation;
operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(@"Image downloaded...");
NSData *imageData = UIImageJPEGRepresentation(image, 90);
[self.arrayOfOwnersImage addObject:imageData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Image download fail: %@", [error localizedDescription]);
}];
[operation start];
}
有什么想法吗?
最佳答案
我认为您应该采用两种不同的方法来实现这一目标。
第一个简单的方法。使用计数器。使用您需要运行的操作数设置计数器。当操作完成时,计数器将递减。当它为零时,操作完成。确保在操作的完成 block 中递减计数器,并确保在同一线程中执行递减。相反,你会遇到竞争条件问题。
第二种更干净的方式。子类 AFHTTPClient
并使用 – enqueueBatchOfHTTPRequestOperationsWithRequests:progressBlock:completionBlock:
方法。来自 AFNetworking 文档。
Creates and enqueues an AFHTTPRequestOperation to the HTTP client’s operation queue for each specified request object into a batch. When each request operation finishes, the specified progress block is executed, until all of the request operations have finished, at which point the completion block also executes.
NSMutableArray *operations = [NSMutableArray array];
for (id element in elements) {
NSMutableURLRequest *request = // your request here
AFHTTPRequestOperation *operation = [[YourAFHTTPClientSubclass sharedClient] HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
//
// Handle success here
//
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//
// Handle error here
//
}];
//
// Add all operations to the operations NSArray
//
[operations addObject:operation];
}
//
// Pass off operations array to the sharedClient so that they are all executed
//
[[YourAFHTTPClientSubclass sharedClient] enqueueBatchOfHTTPRequestOperations:operations progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"Completed %d of %d create operations", numberOfCompletedOperations, totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
//
// All operations are completed
//
}];
编辑
在第一个操作中创建一个 dict 元素数组,如下所示
NSMutableArray *elements = [NSMutableArray array];
for (int i = 1; i <= self.numOfWallPosts; i++)
{
NSString *sharedKey = [NSString stringWithFormat:@"wall_number_%d", i];
NSMutableDictionary *aaa = [[NSMutableDictionary alloc] init];
aaa = [self.sharedPrefs objectForKey:sharedKey];
[elements addObject:aaa];
}
[self handleWallPostsWithWallPost:elements];
现在,handleWallPostsWithWallPost:
将包含我提供的代码。
elements
是传递给方法的元素字典数组。 for
循环允许您创建图像操作。
关于ios - AFHTTPRequestOperation 完成后如何执行函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16884144/