2016-11-23 18 views
0

私は自分のCoreDataManagerクラスを持ち、コアデータオブジェクトを初期化しています。 しかし、以下のコードを実行すると、スローしてエラーが発生し、アプリがクラッシュする可能性があります。NSPersistentStoreCoordinatorには永続ストアがありません(開けません)

NSPersistentStoreCoordinatorには永続ストア(開くことができない)

私はそれがpersistentStoreオブジェクトが実際にゼロであることを示すコードをデバッグ有していません。ここ マイCoreDataManager.mファイル

+ (id)sharedInstance { 
    static CoreDataManager *instance_ = nil; 

    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     instance_ = [[self alloc] init]; 
    }); 
    return instance_; 
} 

- (NSManagedObjectContext *)managedObjectContext { 

    if (managedObjectContext != nil) { 
     return managedObjectContext; 
    } 

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
    if (coordinator != nil) { 
     managedObjectContext = [NSManagedObjectContext new]; 
     [managedObjectContext setPersistentStoreCoordinator: coordinator]; 
    } 
    return managedObjectContext; 
} 


- (NSManagedObjectModel *)managedObjectModel { 

    if (managedObjectModel != nil) { 
     return managedObjectModel; 
    } 
    managedObjectModel = [[NSManagedObjectModel alloc]initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"AppTutor" withExtension:@"momd"]]; 
    return managedObjectModel; 
} 


- (NSURL *)applicationDocumentsDirectory 
{ 
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 
} 


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

    if (persistentStoreCoordinator != nil) { 
     return persistentStoreCoordinator; 
    } 

    NSString *documentsStorePath = 
    [[[self applicationDocumentsDirectory] path] stringByAppendingPathComponent:@"AppTutor.sqlite"]; 

    // if the expected store doesn't exist, copy the default store 
    if (![[NSFileManager defaultManager] fileExistsAtPath:documentsStorePath]) { 
     NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"AppTutor" ofType:@"sqlite"]; 
     if (defaultStorePath) { 
      [[NSFileManager defaultManager] copyItemAtPath:defaultStorePath toPath:documentsStorePath error:NULL]; 
     } 
    } 

    persistentStoreCoordinator = 
    [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 

    // add the default store to our coordinator 
    NSError *error; 
    NSURL *defaultStoreURL = [NSURL fileURLWithPath:documentsStorePath]; 
    NSPersistentStore *store = [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                     configuration:nil 
                        URL:defaultStoreURL 
                       options:nil 
                       error:&error]; 
    if (store == nil) { 

     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    } 

    // setup and add the user's store to our coordinator 
    NSURL *userStoreURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"AppTutor.sqlite"]; 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                configuration:nil 
                  URL:userStoreURL 
                 options:nil 
                  error:&error]) { 

     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    } 

    return persistentStoreCoordinator; 
} 

答えて

1

はあなたがaddPersistentStoreWithType関数にこれらのオプションを追加しようとすることはできますか?

NSMutableDictionary *options = [[NSMutableDictionary alloc] init]; 
[options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption]; 
[options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption]; 

[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
               configuration:nil 
                 URL:userStoreURL 
                options:options 
                 error:&error] 
+0

ありがとうございます。今、私のコアデータは完全に実行されています。 –

+0

あなたは大歓迎です:) –

関連する問題