1
我想在後臺線程上創建一個CoreData獲取請求,以便爲用戶提供取消它的選項。在另一個線程上執行獲取請求
下面是我的後臺線程代碼:
- (void)searchDailyNotes
{
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"DailyNotes"
inManagedObjectContext:self.managedObjectContext];
NSString *searchString = [self.searchTextField stringValue];
NSFetchRequest *fetchRequest= [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"notes contains[cd] %@", searchString];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
[_dailyNotesArray addObjectsFromArray:
[self.managedObjectContext
executeFetchRequest:fetchRequest
error:&error]];
NSLog(@"dailynotesArray count: %lu", [_dailyNotesArray count]);
if(error){
[NSApp presentError:error];
}
[fetchRequest release];
}
問題:
- 如果用戶希望取消搜索,這將是終止後臺線程的corrrect方式?
- 如果我在
managedObjectContext
當前正在提取時中止線程,那麼分配fetchRequest
會發生什麼情況?我會泄漏嗎?
您可能會對此係列文章感興趣:http://www.cimgf.com/2011/05/04/core-data-and-threads-without-the-headache/ – 2011-06-02 15:53:16