CoreData: ошибка: -addPersistentStoreWithType: конфигурация SQLite: (null)

Я получаю эту ошибку каждый раз, когда я устанавливаю приложение на 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.)

Вот как я создаю NSManagedObjectModel и NSPersistentStoreCoordinator

- (NSManagedObjectModel *)managedObjectModel
{
    if (!_managedObjectModel)
    {
        // It is created from the application 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;
}

Я пробовал всевозможные флаги, но я не знаю, как это преодолеть.

Спасибо!

Ответ 1

Я нашел решение своей проблемы. У меня было два потока, обращающихся к NSPersistentStoreCoordinator, пока он еще не был создан. Поэтому я добавил @synchronize в ленивый init и поставил в очередь запросы.

Ответ 2

Дуглас прав. Для 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;
}