0

我搜索了很多,看到了一些答案,但我仍然不确定如何解决它:我只想在操作完成后执行一些 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];
}

有任何想法吗?

4

1 回答 1

1

我认为您应该遵循两种不同的方法来实现这一目标。

第一种简单的方法。使用计数器。使用您需要运行的操作数设置计数器。当操作完成时,计数器将递减。当它为零时,操作完成。确保在操作的完成块中递减计数器,并确保在同一线程中执行递减。相反,您将遇到比赛条件问题。

第二种更干净的方式。子类AFHTTPClient和使用– enqueueBatchOfHTTPRequestOperationsWithRequests:progressBlock:completionBlock:方法。来自 AFNetworking 文档。

为每个指定的请求对象创建一个 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循环允许您创建图像操作。

于 2013-06-02T15:47:10.917 回答