2

I'm developing a TodayExtension for a productivity app of task lists and I need to show a list of some tasks in a table inside the TodayExtension.

I don't have any problem setting up the table view but I have a crash when I'm trying to set up MagicalRecord and CoreData.

My source code for the method that crash is:

- (void)setupCoreDataStackWithStoreNamed:(NSString *)storeNamed
{
    if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil)
    {
        return;
    }

    NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
    NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

    NSString *containerPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP].path;
    NSString *sqlitePath = [NSString stringWithFormat:@"%@/%@", containerPath, storeNamed];
    NSURL *storeURL = [NSURL URLWithString:sqlitePath];

    [psc MR_addSqliteStoreNamed:storeURL withOptions:nil];
    [NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:psc];
    [NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:psc];
}

And the line with the crash is:

    [psc MR_addSqliteStoreNamed:storeURL withOptions:nil];

The error message is:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'CoreData SQL stores only support file URLs (got /private/var/mobile/Containers/Shared/AppGroup/66A40F3A-9E25-42BD-9BD0-D86BC5A93FBD/myappname_560967e1665e88ab37000000.sqlite).'

I got the inspiration from this post How to access CoreData model in today extension (iOS)

Community
  • 1
  • 1
EnriMR
  • 3,924
  • 5
  • 39
  • 59

1 Answers1

1

You are passing a NSString url (/../...) to the MR_addSqliteStoreNAamed, but the crash tell you that you need to pass a file url (file://...)

So you have to change this source code:

NSString *containerPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP].path;
NSString *sqlitePath = [NSString stringWithFormat:@"%@/%@", containerPath, storeNamed];
NSURL *storeURL = [NSURL URLWithString:sqlitePath];

to this:

NSURL *storeURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP];
storeURL = [storeURL URLByAppendingPathComponent:storeNamed];

Another solution could be do that:

NSString *pathToYourSqliteFile = @"/your/path/here";
NSURL *storeURL = [NSURL fileURLWithPath: isDirectory:NO];
Manuel Martín
  • 338
  • 4
  • 8