Reputation: 8695
I made an iOS 8 app that uses the new app groups feature to share a Core Data store with an extension. It worked so well that I thought I would try it with 2 separate apps sharing a Core Data store in an App Group container. But while it worked between App and Extension, I am getting Core Data store corruptions issues when sharing with 2 apps.
Depending on the order in which I open the 2 apps, I get different errors:
Fetches cause this error:
CoreData: error: (522) I/O error for database at /private/var/mobile/Containers/Shared/AppGroup/[…].sqlite. SQLite error code:522, 'not an error’
Saves cause this error:
CoreData: error: (11) Fatal error. The database at /private/var/mobile/Containers/Shared/AppGroup/[...].sqlite is corrupted. SQLite error code:11, 'database disk image is malformed’
Or:
Core Data: error: -executeRequest: encountered exception = error during SQL execution : PRIMARY KEY must be unique with userInfo = { NSFilePath = "/private/var/mobile/Containers/Shared/AppGroup/[...].sqlite"; NSSQLiteErrorDomain = 19; } CoreData: error: (19) PRIMARY KEY must be unique
Upvotes: 7
Views: 2033
Reputation: 1
Here's how I did it by creating a framework to hold/manage the database
Use it to create your databaseURL using:
[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:
database = [ [UIManagedDocument alloc] initWithFileURL: databaseURL]
[database saveToFileURL: database.fileURL forSaveOperation: UIDocumentSaveForCreating completionHandler: ^(BOOL success)
persistentContainer
.managedObjectModel
has to be defined by the framework. It cannot be in any of the apps. They can see it, but it has to be part of the framework's bundle.
That's the basic outline and there is a lot more to it, but with this you should be able to get there. Have fun!Upvotes: 0