Accessing Core Data SQL Database in iOS 8 Extension (Sharing Data Between App and Widget Extension) Accessing Core Data SQL Database in iOS 8 Extension (Sharing Data Between App and Widget Extension) sqlite sqlite

Accessing Core Data SQL Database in iOS 8 Extension (Sharing Data Between App and Widget Extension)


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];}