NSManagedObjectContext
is not thread safe. If you creating context on main thread
, then you can access the context only on the main thread
So you have to run the
executeFetchRequest
in the main thread, instead you can use performBlock
which will schedule it to run on its own thread.
//performblock将安排它运行在它自己的线程
- (NSArray *)executeFetchRequest:(NSFetchRequest *)request inContext:(NSManagedObjectContext *)context
{
__block NSArray *results = nil;
[context performBlockAndWait:^{
NSError *error = nil;
results = [context executeFetchRequest:request error:&error];
}];
return results;
}
performBlock won't work if you're initialising the context in the older
NSConfinementConcurrencyType
confinement model.