0

私は夢中になるかもしれないと思います。次のメソッドでは、別のコード行を追加しない限り、戻り値_persistentStoreCoordinatorはnilです。チェック_persistentStoreCoordinator == nilするだけで、そうでないことを確認できます。(NSLogステートメントでもうまくいきます。)

psc別の行を追加しない場合、ブレークポイントを使用して検査する場合は常にnil以外ですが、メソッドの最後の行で_persistentStoreCoordinatorはnilです。

最も奇妙な(またはおそらく最も役立つ?)ことは、問題が始まったときにこのクラスに変更を加えなかったことです。

どんな助けや説明も大歓迎です!

- (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;
}
4

1 に答える 1

1

.h ファイルを再確認すると、_persistantStoreCoordinator への弱い参照を維持していたことがわかりました。

@property (weak, nonatomic, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

案の定、強い固定のものへの参照を変更します。

于 2012-09-19T00:58:07.640 に答える