我需要保護我的代碼的關鍵區域,它是多線程的。我想阻止它在另一個線程完成之前被多次調用。這是我與合作:保護關鍵代碼不被再次調用
- (void) filterAllEventsIntoDictionary{
// start critical area
if (self.sortedKeys.count != 0) {
[self.sortedKeys removeAllObjects];
}
dispatch_async(self.filterMainQueue, ^{
[self internal_filterAllEventsIntoDictionary];
dispatch_sync(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
由於internal_filterAllEventsIntoDictionary
方法還訪問self.sortedKeys
,如果這個代碼被調用了兩次,它在開始崩潰,因爲。
我還需要在另一個線程中調用internal...
方法,因爲我不想阻塞UI。那麼在dispatch_async
調用仍未完成時阻止此方法開始的最佳方法是什麼?
感謝您指出正確的方向。 –