如果我在主线程上创建了NSFetchRequest
,如下所示:
NSManagedObject *bar = ...;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Foo"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"SELF.bar == %@",
[bar objectID]]];
将包含包含
NSFetchRequest
的NSPredicate
的NSManagedObjectID
传递给后台线程是否安全?NSManagedObject *bar = nil;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Foo"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"SELF.bar == %@",
[bar objectID]]];
NSPersistentStoreCoordinator *persistentStoreCoordinator = ...;
[[NSBlockOperation blockOperationWithBlock:^{
NSManagedObjectContext *managedObjectContext = [NSManagedObjectContext new];
[managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];
NSArray *foos = [managedObjectContext executeFetchRequest:fetchRequest
error:NULL];
}] start];
最佳答案
我在CoreData release notes for iOS 5中找到了一些示例代码,几乎可以做到这一点,所以看起来还可以。
NSFetchRequest *fr = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];
__block NSUInteger rCount = 0;
[context performBlockAndWait:^() {
NSError *error;
rCount = [context countForFetchRequest:fr error:&error];
if (rCount == NSNotFound) {
// Handle the error.
}
}];
NSLog(@"Retrieved %d items", (int)rCount);
关于objective-c - 具有NSPredicate且包含NSManagedObjectID的NSFetchRequest是否可以安全地跨线程边界传递?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13166240/