如果我在主线程上创建了NSFetchRequest
,那么:
NSManagedObject *bar = ...;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Foo"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"SELF.bar == %@",
[bar objectID]]];
将此NSFetchRequest
与包含NSPredicate
的{{1}}传递给后台线程是否安全?
NSManagedObjectID
答案 0 :(得分:1)
我在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);