我建議使用塊來接收來自異步調用的響應。在這種情況下,您可以在塊中執行數據庫操作。
dispatch_async(dispatch_get_main_queue());
不是爲異步調用。它主要用於更新UI。
所有的異步調用都需要在後臺線程上完成。
來到你的問題,你可以做這樣的事情:
-(void)checkToDeleteWithCompletion:(MyCompletion)completion{
__block NSMutableArray *responseArray = [NSMutableArray array];
// Create a dispatch group
dispatch_group_t group = dispatch_group_create();
for (int i = 0; i < yourArray.count; i++) {
// Enter the group for each request we create
dispatch_group_enter(group);
[self ..methodToPErformAsynccallWithCOmpleton success:^(BOOL success, id response) {
dispatch_group_leave(group);
[responseArray addObject:response];
} failure:^(NSError *error, NSInteger statusCode) {
dispatch_group_leave(group);
}];
}
// Here we wait for all the requests to finish
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// Do whatever you need to do when all requests are finished
completion(YES,[responseArray copy]);
});
}
最後,你可以使用對responseArray循環,刪除不需要的對象。
您也可以通過限制併發呼叫的數量:
dispatch_semaphore_create(maxConcurrencyValue)
或者你可以使用的NSOperation通過rmaddy的建議!
你打算循環訪問100條記錄,詢問遠程服務「我可以刪除這個嗎?」 - 每次都在等待迴應? – DonMag
是的,這是正確的。 –
使用塊接收異步響應,並在收到響應時在塊中執行sqlite操作。 –