Ios CoreData:错误:-addPersistentStoreWithType:SQLite配置:(null)

Ios CoreData:错误:-addPersistentStoreWithType:SQLite配置:(null),ios,core-data,ios8,xcode6,Ios,Core Data,Ios8,Xcode6,每次使用Xcode 6在Simulator上安装应用程序时,我都会遇到这个错误 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

每次使用
Xcode 6
Simulator
上安装应用程序时,我都会遇到这个错误

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.)
这就是我如何创建
NSManagedObjectModel
NSPersistentStoreCoordinator

- (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
,而它还没有创建。因此,我在lazy init上添加了一个
@synchronize
,并将请求排队。

道格拉斯是对的。对于像我这样的iOS Noobie,措辞如下:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    @synchronized(self) {

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

感谢@Douglas提供的解决方案,我在Swift 4中遇到了同样的问题

我所做的是在
persistentStoreCoordinator
之间添加
objc\u sync\u enter
objc\u sync\u exit

例如:

 lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {

        objc_sync_enter(self)

        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
            let url = self.applicationDocumentsDirectory.appendingPathComponent("moduleName.sqlite")
            var failureReason = "There was an error creating or loading the application's saved data."
            do {
                try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]) // maybe change after pre - production
            } catch {
                // Report any error we got.
                var dict = [String: AnyObject]()
                dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject
                dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject
                dict[NSUnderlyingErrorKey] = error as NSError
                let wrappedError = NSError(domain: "ERROR_DOMAIN", code: 9999, userInfo: dict)

                #if DEBUG
                NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
                #endif
                abort()
            }

        objc_sync_exit(self)

        return coordinator
    }()

我想知道这是否是一个路径问题。如果使用
NSDocumentDirectory
而不是
NSLibraryDirectory
,您会得到同样的结果吗?我得到了一个不同的错误。。我觉得更糟
Error Domain=nscocaerorrordomain code=512“操作无法完成。(Cocoa错误512)。”UserInfo=0x7fbec9c36e70{reason=Failed to create file;code=2},操作无法完成。(可可错误512。)
这把锁对我来说非常合适。我的问题是并行单元测试。