我想我可能要疯了。在以下方法中,返回值 _persistentStoreCoordinator 为 nil ,除非我添加另一行代码。检查_persistentStoreCoordinator == nil
足以确保它不是。(NSLog 语句也可以解决问题。)
如果我不添加另一行,则 _persistentStoreCoordinator 在方法的最后一行中为零,即使使用断点检查它时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;
}