0

オブジェクトを非同期でダウンロードし、配列に保存します。次に、各オブジェクトについて、ジオコーディングを使用して座標をダウンロードし (これも非同期です)、座標である新しいパラメーターを使用して各オブジェクトのデータベースを更新します。私の方法は次のようになります。

- (void)downloadObjectsWithTitle:(NSString *)title andHandler:(void(^)(NSMutableDictionary *result))handler {
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
                                                        path:nil
                                                  parameters:nil];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        //I get here array of objects
        //now for each object I want to download geocoding localization so i called another asynchronyous method getLocationWithTitle:andHandler;
       for(int i = 0; i < resutArray.count; i++) {
           [self downloadLocationWithString:[dictionary objectForKey:@"string"] andHandler:^(NSMutableDictionary *result) {
               //update database;
           }];
        }
        handler(dictionary);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    [operation start];
}

私の質問は、各オブジェクトの座標をダウンロードして起動する方法です。

handler(dictionary);

そのため、各座標のダウンロード (オブジェクトごと) を待ってから、メソッドを終了します (ハンドラーを起動します)。

すべての提案に感謝します。

4

3 に答える 3

1

同時キューdispatch_asyncで inを使用していると仮定します。downloadLocationWithString:

dispatch_barrier_async(queue, ^{
    // will only be called after all the blocks submitted to queue have finished.
}];

(シリアル キューを使用している場合は、最後のブロックの最後の行でハンドラーを呼び出すだけです)

于 2013-06-22T07:36:53.993 に答える