Problem:
Unable to access application's Core Data database from within a Widget Extension in the Today View.
The app itself is able to read and write to the database as per normal under iOS 8, but the extension will fail to create the store, giving the error, unable to write to file.
The log is the following:
Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)"
reason = "Failed to create file; code = 2
Widgets are unable to access the NSDocuments directory, which is where one would normally store their database.
The solution is to first create an App Group
Go to: Project - Target - App Groups - Add New Container
Name the container, i.e. 'group.mycontainer'
Repeat the process for the Widget's Target using the same name for the container.
Then write your database to your group container.
So:
NSURL *storeURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSAllDomainsMask] lastObject]; storeURL = [storeURL URLByAppendingPathComponent:@"db.sqlite"];
Becomes:
NSURL *storeURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.mycontainer"]; storeURL = [storeURL URLByAppendingPathComponent:@"db.sqlite"];
And initialising the store should be like so:
NSURL *storeURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.mycontainer"]; storeURL = [storeURL URLByAppendingPathComponent:@"db.sqlite"]; NSPersistentStore *store = nil; store = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]
Just figured out that the app group files do not get backed up using the standard iOS backup procedure.
Keep in mind that the user may lose all their app data after restoring iOS if you keep the persistent store in an app group container.
UPDATE
rdar://18750178
UPDATE
seems like fixed in iOS 8.1, Apple guys messaged me and asked to check the problem in iOS 8.1 whether it fixed or not (quite impudent isn't?). I haven't tested it, so keep in mind. Anyway, keeping storage in AppGroups is a dead idea in case you are supporting defective iOS 8.0
Change
[MagicalRecord setupCoreDataStackWithStoreNamed:@"Database"];
to
- (void)setupCoreDataStack
{
if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil)
{
return;
}
NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
NSURL *storeURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.yourgroup"];
storeURL = [storeURL URLByAppendingPathComponent:@"Database.sqlite"];
[psc MR_addSqliteStoreNamed:storeURL withOptions:nil];
[NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:psc];
[NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:psc];
}
The same for Swift:
private func setupCoreDataStack() {
if NSPersistentStoreCoordinator.MR_defaultStoreCoordinator() != nil {
return
}
let managedObjectModel = NSManagedObjectModel.MR_defaultManagedObjectModel()
let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)
var storePath = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier(PBOSharedSuiteGroupName)
storePath = storePath!.URLByAppendingPathComponent("AppName.sqlite")
var error: NSError?
persistentStoreCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storePath, options: nil, error: &error)
NSPersistentStoreCoordinator.MR_setDefaultStoreCoordinator(persistentStoreCoordinator)
NSManagedObjectContext.MR_initializeDefaultContextWithCoordinator(persistentStoreCoordinator)
}
Remember to attach this method to both: AppDelegate and Today Extension
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With