Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null)

I got this error every time that I install the app on Simulator using 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.)

This is how I create NSManagedObjectModel and 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;
}

I've tried every kind of flags.. but I don't know how to get over it.

Thanks!

like image 481
Douglas Ferreira Avatar asked Sep 27 '14 14:09

Douglas Ferreira


People also ask

How do I clear my Core Data?

One approach to delete everything and reset Core Data is to destroy the persistent store. Deleting and re-creating the persistent store will delete all objects in Core Data.

How do I find my Core Data?

Let's start with a blank Xcode project. Open Xcode and create a new project by choosing the Single View App template form the iOS > Application section. Name the project Notes and check Use Core Data at the bottom. Open AppDelegate.

What is faulting in Core Data?

Faulting allows Core Data to put boundaries on the object graph. Because a fault is not realized, a managed object fault consumes less memory, and managed objects related to a fault are not required to be represented in memory at all.

How do I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.


2 Answers

I found the solution to my problem. I had two threads accessing the NSPersistentStoreCoordinator while it's was not created yet. So I added a @synchronize on the lazy init and queued the requests.

like image 195
Douglas Ferreira Avatar answered Sep 29 '22 12:09

Douglas Ferreira


I had the same problem in Swift 4, Thanks to @Douglas for the solution

What i did was adding objc_sync_enter and objc_sync_exit between the persistentStoreCoordinator.

Example:

 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
    }()
like image 21
Maor Avatar answered Sep 29 '22 11:09

Maor