我搜索了很多并看到了一些答案,但我仍然不确定如何解决它:我只想在操作完成后执行一些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];
- 在后台下载一些图像,因此,块已完成,但在后台,有一些图像下载。
我怎么知道图像下载何时完成?
- (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];
}
有什么想法吗?
答案 0 :(得分:1)
我认为您应该采用两种不同的方式来实现这一目标。
第一个简单的方法。使用计数器。使用您需要运行的操作数设置计数器。当操作完成时,计数器将递减。当它为零时,操作就完成了。确保在操作的完成块中递减计数器,并确保在同一个线程中执行递减。相反,你会遇到竞争条件问题。
第二种更干净的方式。子类AFHTTPClient
并使用– enqueueBatchOfHTTPRequestOperationsWithRequests:progressBlock:completionBlock:
方法。来自AFNetworking doc。
将AFHTTPRequestOperation创建并排入HTTP客户端 每个指定的请求对象的操作队列成批。什么时候 每个请求操作完成,指定的进度块为 执行,直到所有请求操作完成,在此处执行 指向完成块也执行。
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
循环允许您创建图像操作。