我收到了这次崩溃:
Fatal Exception: NSInvalidArgumentException
*** setObjectForKey: object cannot be nil (key: managedObjectContext)
我试图找出原因。我不确定这种情况发生的原因和根本原因。有人可以帮忙吗?这是崩溃日志:
Fatal Exception: NSInvalidArgumentException
0 CoreFoundation 0x19058e1c0 __exceptionPreprocess
1 libobjc.A.dylib 0x18efc855c objc_exception_throw
2 CoreFoundation 0x19046f79c -[__NSCFNumber hash]
3 CoreData 0x19290b13c -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:deletions:updates:refreshes:deferrals:wasMerge:]
4 CoreData 0x192883340 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:]
5 CoreData 0x19287bc30 _performRunLoopAction
6 CoreFoundation 0x19053b7dc __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
7 CoreFoundation 0x19053940c __CFRunLoopDoObservers
8 CoreFoundation 0x19053989c __CFRunLoopRun
9 CoreFoundation 0x190468048 CFRunLoopRunSpecific
10 GraphicsServices 0x191eee198 GSEventRunModal
11 UIKit 0x1964542fc -[UIApplication _run]
12 UIKit 0x19644f034 UIApplicationMain
13 MyApp 0x10013ff88 main (main.m:15)
14 libdispatch.dylib 0x18f44c5b8 (Missing)
以下是CoreData创建的代码:
- (NSManagedObjectContext *)managedObjectContext
{
if (appDelegate.managedObjectContext != nil) {
return appDelegate.managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
appDelegate.managedObjectContext = [[NSManagedObjectContext alloc] init];
[appDelegate.managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return appDelegate.managedObjectContext;
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
@try {
if (appDelegate.managedObjectModel != nil) {
return appDelegate.managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"AppDatabase" withExtension:@"momd"];
appDelegate.managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
} @catch (NSException *exception) {
LogError(self, NSStringFromSelector(_cmd), @"EXCEPTION!\n\n%@", exception);
}
return appDelegate.managedObjectModel;
}
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
@try {
if (appDelegate.persistentStoreCoordinator != nil) {
return appDelegate.persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:VERSION_SQLITEDB];
NSError *error = nil;
appDelegate.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![appDelegate.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
error:&error]) {
if (error) {
LogError(self, NSStringFromSelector(_cmd), @"ERROR!\n\n%@", error);
CustomAlertView *alertView = [[CustomAlertView alloc] initWithTitle:nil message:ERRORMESSAGE_APPERROR delegate:nil twoButtonView:NO];
[alertView show];
}
}
} @catch (NSException *exception) {
LogError(self, NSStringFromSelector(_cmd), @"EXCEPTION!\n\n%@", exception);
}
return appDelegate.persistentStoreCoordinator;
}
更新
这是我认为崩溃发生的地方,当我获得NSManagedObjectContext引用时:
- (NSManagedObjectContext *)managedObjectContextRef {
NSPersistentStoreCoordinator* coordinator = [self persistentStoreCoordinator];
NSManagedObjectContext* context;
@try {
if (coordinator != nil) {
context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[context setPersistentStoreCoordinator:coordinator];
}
} @catch (NSException *exception) {
LogError(self, NSStringFromSelector(_cmd), @"EXCEPTION!\n\n%@", exception);
}
return context;
}
这会有帮助吗?