ios - NSPersistentStoreCoordinator没有持久性存储(无法打开)

标签 ios objective-c core-data

我有我的CoreDataManager类和初始化核心数据对象。
但是,当下面的代码运行时,它将引发错误并导致应用程序崩溃。

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

最佳答案

您可以尝试将这些选项添加到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]

关于ios - NSPersistentStoreCoordinator没有持久性存储(无法打开),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40765989/

相关文章:

ios - 使用 bluemix for ios 的静默远程通知

objective-c - 为什么 GHUnit 中的异步测试中的错误断言会导致应用程序崩溃,而不仅仅是测试失败?

iphone - 将对象设置为 nil 与在 dealloc 中向其发送释放消息有什么区别

ios - 与网络服务器同步核心数据

ios - 将 NSDate 设置为 NSManagedObject 慢吗?

iphone - 用于 Objective-C 的 Orkut/Flickr 框架

ios - 如何使用 ios 图表从自定义方法中突出显示条形图中的条形图?

ios - 没有得到 "MAP Route"

iphone - UITextView 委托(delegate)方法 textView :shouldChangeTextInRange:replacementText: gives erroneous replacementText on fast delete

iphone - 如何不保留从 NSManagedObjectContext 检索到的 NSManagedObjects