2012-10-23 75 views
0

執行一些任務在後臺我想在後臺執行一些數據庫相關的任務,我已經添加了代碼如何在iOS中

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 
              0), ^(void) { 
     [lclDB deleteRecoredwithBlock:^(BOOL success) { 
      if (success) { 
       NSLog(@"Deletion Succesful..."); 
      } 
     }]; 
    }); 

deleteRecord函數內部調用的方法數順序來執行本地database.now刪除操作我一直等到所有刪除操作執行完畢。但我想在後臺執行這個完整的刪除操作。如果有任何已知的請幫助我弄清楚這些問題。

+0

你打電話是正確的。您正在啓動'dispatch_async'塊。雖然它正在異步刪除記錄,但您可以繼續執行其他操作。 – Ilanchezhian

+0

在這段代碼後面加上'NSLog'。它將在「Deletio Succesful ...」之前打印。 – Ilanchezhian

+0

你檢查了我的答案嗎? – TheTiger

回答

0

嘗試performSelectorInBackground:withObject:方法。

0
[self performSelectorInBackground:@selector(backgroundMethod) withObject:nil]; 

您也可以使用NSInvocationOperation

NSOperationQueue *queue = [NSOperationQueue new]; 
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(deleteDataWithOperation) object:nil]; 
[queue addOperation:operation]; 

這是你的deleteDataWithOperation方法 -

-(void)deleteDataWithOperation 
{ 
    //Do your work here 
}