NSPersistentStoreCoordinator

NSPersistentStoreCoordinator

每次我使用SimulatorXcode 6上安装应用程序时,都会收到此错误

2014-09-27 11:25:01.286 MyFace[2992:1780149] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/douglasferreira/Library/Developer/CoreSimulator/Devices/DAC1BEDE-8673-471C-ADFD-923654C78719/data/Containers/Data/Application/D2213EE4-3807-44FF-9FD0-E7C6C1BD18A2/Library/MyFace.sqlite options:{
NSInferMappingModelAutomaticallyOption = 1;
NSMigratePersistentStoresAutomaticallyOption = 1;
} ... returned error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t    be completed. (Cocoa error 512.)" UserInfo=0x7fe566317030 {reason=File appeared during sanity check; this seems suspicious} with userInfo dictionary {
reason = "File appeared during sanity check; this seems suspicious";
}

[2014-09-27 11:25:01:288] [ERROR] Problems to initialize persistent store coordinator: Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x7fe566317030 {reason=File appeared during sanity check; this seems suspicious}, The operation couldn’t be completed. (Cocoa error 512.)

这就是我创建NSManagedObjectModelNSPersistentStoreCoordinator的方法
- (NSManagedObjectModel *)managedObjectModel
{
    if (!_managedObjectModel)
    {
        // It is created from the application's model with the following name and extension
        NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyFace" withExtension:@"momd"];
        _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    }
    return _managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (!_persistentStoreCoordinator)
    {
        // generic variable to hold any error occurred during context creation
        NSError *error = nil;

        NSDictionary *persistentOptions = @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES};

        // Create the coordinator with the previous parameters
        NSURL *storeURL = [[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
        storeURL = [storeURL URLByAppendingPathComponent:@"MyFace.sqlite"];

        // try to initialize persistent store coordinator with options defined below
        self.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
        [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                      configuration:nil
                                                                URL:storeURL
                                                            options:persistentOptions
                                                              error:&error];

        if (error)
        {
            NSLog(@"[ERROR] Problems to initialize persistent store coordinator: %@, %@", error, [error localizedDescription]);
        }
    }
    return _persistentStoreCoordinator;
}

我已经尝试过各种标志..但我不知道如何克服它。

谢谢!

最佳答案

我找到了解决我问题的方法。
我还没有两个线程来访问NSPersistentStoreCoordinator,但尚未创建。因此,我在懒惰的init上添加了@synchronize并将请求排入队列。

关于ios - CoreData : error: -addPersistentStoreWithType:SQLite configuration:(null),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26075724/

10-13 05:16