Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MagicalRecord (CoreData) + Today Extension (iOS8)... Will They Play?

Hoping you can help. I'm adding Today support to my app, which uses MagicalRecord https://github.com/magicalpanda/MagicalRecord to managing all my CoreData stuff.

I'm tearing my hair out trying to understand how to surface my data into the Today extension.

I have enabled app groups as outlined here http://blog.sam-oakley.co.uk/post/92323630293/sharing-core-data-between-app-and-extension-in-ios-8 however all the documentation and StackOverflow posts I'm reading relate to using CoreData directly. MagicalRecord does a lot of the hard work for you, which is why I used it as I was totally new to it all at the beginning of this project. So things like:

Where you initialise your Core Data stack, you’ll be adding a store to your persistentStoreCoordinator a little something like this:

[persistentStoreCoordinator
addPersistentStoreWithType:NSSQLiteStoreType configuration:nil
URL:storeURL options:options error:&error]

It’s simply a matter of changing your previous value for storeURL (usually somewhere in NSDocumentDirectory) to a location contained in your shared App Group folder. You do this using

containerURLForSecurityApplicationGroupIdentifier: NSURL *directory =
[[NSFileManager defaultManager]
containerURLForSecurityApplicationGroupIdentifier:@"group.YourGroupName"];
NSURL *storeURL = [directory 
URLByAppendingPathComponent:@"YourAppName.sqlite"];

... I'm not understanding how / where to implement.

I'd imagined I'd just have to set up the MagicalRecord stack in my extension as I do in my appDelegate, but of course it's failing.

Really hoping someone might be in a similar situation and be able to shed some light on how to move forward with this one.

Any code you need to me to post up just let me know.

Thanks in advance

like image 630
Emile Bennett Avatar asked Sep 26 '14 17:09

Emile Bennett


3 Answers

Not sure if this works on previous versions of MagicalRecord, but as of 2.2 you can just pass the final url as the store name:

NSFileManager *fileManager = [[NSFileManager alloc] init];

NSURL *directory = [fileManager containerURLForSecurityApplicationGroupIdentifier:@"group.yellow"];
NSURL *pathToStore = [directory URLByAppendingPathComponent:kMagicalRecordDefaultStoreFileName];

[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(id)pathToStore];
like image 61
José Manuel Sánchez Avatar answered Oct 22 '22 23:10

José Manuel Sánchez


I had the same problem I was able to fix it by following this thread. https://github.com/magicalpanda/MagicalRecord/issues/858

I first updated the following method in NSPersistentStore+MagicalRecord.m

- (NSURL *) MR_urlForStoreName:(NSString *)storeFileName
{
  NSFileManager *fileManager = [[NSFileManager alloc] init];

  NSURL *directory = [fileManager containerURLForSecurityApplicationGroupIdentifier:@"group.yourIdentifier"];
  NSURL *pathToStore = [directory URLByAppendingPathComponent:storeFileName];

  return pathToStore;

// NSArray *paths = [NSArray arrayWithObjects:[self MR_applicationDocumentsDirectory], [self MR_applicationStorageDirectory], nil];
// NSFileManager *fm = [[NSFileManager alloc] init];
//

// for (NSString *path in paths) 
// {
// NSString *filepath = [path stringByAppendingPathComponent:storeFileName];
// if ([fm fileExistsAtPath:filepath])
// {
// return [NSURL fileURLWithPath:filepath];
// }
// }
//
// return [NSURL fileURLWithPath:[[self MR_applicationStorageDirectory] stringByAppendingPathComponent:storeFileName]];
}

Then within my extension I just added the following to its view did load method.

- (void)viewDidLoad {
  [super viewDidLoad];
  [MagicalRecord setupCoreDataStackWithStoreNamed:<storeFileName>];
}
like image 36
rowan.t Avatar answered Oct 23 '22 01:10

rowan.t


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];
}
like image 3
Igor Avatar answered Oct 23 '22 00:10

Igor