我想我可能会发疯。在以下方法中,返回值_persistentStoreCoordinator为nil ,除非我添加另一行代码。检查_persistentStoreCoordinator == nil
就足以确保它不是。 (NSLog语句也可以解决问题。)
如果我不添加另一行,_persistentStoreCoordinator在方法的最后一行是nil,即使用断点psc
检查它总是非零。
最奇怪的(或者也许是最有帮助的?)事情是,当问题开始时我没有对这个班级做任何改动。
非常感谢任何帮助或解释!
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator == nil) {
NSLog(@"SQLITE STORE PATH: %@", [self pathToLocalStore]);
NSURL *storeURL = [NSURL fileURLWithPath:[self pathToLocalStore]];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *e = nil;
if (![psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:options
error:&e]) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:e forKey:NSUnderlyingErrorKey];
NSString *reason = @"Could not create persistent store.";
NSException *exc = [NSException exceptionWithName:NSInternalInconsistencyException
reason:reason
userInfo:userInfo];
@throw exc;
}
_persistentStoreCoordinator = psc;
if (_persistentStoreCoordinator == nil)
{
NSLog(@"We never reach here.");
}
}
return _persistentStoreCoordinator;
}