+(NSManagedObjectContext *)managedObjectContext {
NSMutableDictionary *managedObjectContexts =BGMDCRManagedObjectContextThreadHandler.singleton.managedObjectContexts;
if (managedObjectContexts[[self threadKey]] == nil ) {
@synchronized(self)
{
NSManagedObjectContextConcurrencyType ct=nil;
if ([NSThread isMainThread]) {
ct = NSMainQueueConcurrencyType;
}
else
{
ct = NSPrivateQueueConcurrencyType;
}
NSManagedObjectContext *threadContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:ct];
threadContext.parentContext = [self managedObjectContextMainContext];//This is always the same moc
threadContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
managedObjectContexts[[self threadKey]] = threadContext;
}
}
NSManagedObjectContext * moc= managedObjectContexts[[self threadKey]]; //Execute bad access here
return moc;
}
我仍然使用旧式的managedObjectContexts管理,其中每个线程都有自己的托管对象上下文。
无论如何,如果(managedObjectContexts [[self threadKey]] == nil)做得很好。
我也做了一些po并得到了:
(lldb) po [self threadKey]
0xcd7b9a0
(lldb) po managedObjectContexts
{
0xcd7b9a0 = "<NSManagedObjectContext: 0x1552d050>";
0xd81fe20 = "<NSManagedObjectContext: 0xcb6f230>";
0xd8dd9d0 = "<NSManagedObjectContext: 0x17406cc0>";
}
(lldb) po managedObjectContexts[[self threadKey]]
<NSManagedObjectContext: 0x1552d050>
显然一切都很好。那么EXC_BAD_ACCESS可能意味着什么?