0

I have added a Today Extension in my existing app and setup a separate core data stack(reusing the same code which is used to setup core data for the main app).
My app data gets deleted when I run the Today extension as if setting up of Today core data stack deletes the existing data. Refer below code for returning persistentCoordinator:

 let options = [NSMigratePersistentStoresAutomaticallyOption:true,
                      NSInferMappingModelAutomaticallyOption:true,
                                       NSSQLitePragmasOption:["journal_mode":"MEMORY"]]

var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
            if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: options, error: &error) == nil {

    NSLog("Unresolved error \(error)")
            }
            return coordinator
Julian E.
  • 4,687
  • 6
  • 32
  • 49
Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
  • It seems your `storeurl` is not correct, make sure it use grouped folder. Your function `applicationDocumentsDirectory` should return the groupPath, in your case, it seems the app is creating a new CoreData, rather than using the correct one. – iphonic Jun 04 '15 at 11:00
  • I am using the shared group identifier to setup core data. The data deletes only if I try to open today(fetch data) and main app is not killed. If the main app is killed Today works fine and no data gets deleted. – Puneet Sharma Jun 04 '15 at 11:01
  • Why are you setting `journal_mode` to `MEMORY`? The SQLite documentation says that this ["...saves disk I/O but at the expense of database safety and integrity."](https://sqlite.org/pragma.html) – Tom Harrington Jun 04 '15 at 16:30
  • I implemented solution mentioned in http://pablin.org/2013/05/24/problems-with-core-data-migration-manager-and-journal-mode-wal/. Still the issue persists, but my data does not. – Puneet Sharma Jun 05 '15 at 05:59
  • That blog post does not say anything about setting the journal mode to `MEMORY`. – Tom Harrington Jun 12 '15 at 03:26
  • I changed the journal_mode to "WAL" and even "DELETE" but that did not resolve the issue. Finally, I figured out that the issue was because of using mergedModelFromBundles method of NSManagedObjectModel. – Puneet Sharma Jun 12 '15 at 07:42

1 Answers1

0

After lots of debugging, and reading googled blogs and docs, I think the issue was because of mergedModelFromBundles method of NSManagedObjectModel.
I had multiple .xcdatamodels in my bundle and during set up of core data stack, I was using the above method to merge a single model.
As core data stack set up for Today, the model returned from the method was different than the one used by Main app to create sqlite store and hence the persistent store returned was different and needed migration.
But migration more often than not was failing and deleting my .sqlite(set up by main app) and was setting up a new sqlite.
One solution I found was to use
NSManagedObjectModel(contentsOfURL: modelPathURL!)
method to get managed object model, where url is the path url of momd file in bundle.
Refer: Core Data Migration Guide

However, I have not yet figured, how to give path url of three different xcdatamodelids as single path url and how to migrate them if in future any xcdatamodel changes.
So, at present I have merged three xcdatamodels into single xcdatamodel in my bundle to solve this issue.

Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33