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

时间:2014-09-27 14:35:41

标签: ios core-data ios8 xcode6

我每次使用Simulator

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

我尝试过各种各样的旗帜......但我不知道如何克服它。

谢谢!

3 个答案:

答案 0 :(得分:11)

我找到了解决问题的方法。 我有两个线程访问NSPersistentStoreCoordinator,但还没有创建它。所以我在lazy init上添加了@synchronize并将请求排队。

答案 1 :(得分:1)

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

我所做的是在objc_sync_enter之间添加objc_sync_exitpersistentStoreCoordinator

示例:

 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
    }()

答案 2 :(得分:0)

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

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