当使用NSPrivateQueueConcurrencyType
和NSMainQueueConcurrencyType
类型NSManagedObjectContext
, 是否可以安全地在同一个上下文中执行嵌套的performBlock调用?嵌套performBlock:在NSManagedObjectContext
[backgroundContext performBlock:^{
NSFetchRequest *myRequest = ...;
__block NSArray *result= nil;
[backgroundContext performBlockAndWait:^{
results = [backgroundContext executeFetchRequest:myRequest error:NULL];
}];
}];
这似乎是愚蠢的,但我有很多其封装executeFetchRequest
来电助手方法现有的代码库。我不想对调用者是否已经使用performBlock做出假设。 例如:
-(void)updateObjects:(BOOL)synchronous
{
if (YES == synchronous)
[self fetchHelper];
else
{
[backgroundContext performBlock:^{
[self fetchHelper];
}];
}
}
-(NSArray*)fetchHelper
{
[self.backgroundContext performBlockAndWait:^{
//Fetch the objects...
[self.backgroundContext executeFetchRequest: (...)];
}];
}
我试过了,它的工作原理。但我已经学会了(很难)对Core Data和多线程非常小心。
什么performBlock,是重入吗? – malhal
这不是,这是在会议中讨论。如果您调用performBlock,您的请求将排队,因为它是异步的。 –
只是要清楚,OP在第二位代码中所做的是可以做到的,但是如果两种方法都有“performBlock”,会导致问题?这是正确的方式来看待这个? – hokkuk