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
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];
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>];
}
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];
}
来源:https://stackoverflow.com/questions/26065539/magicalrecord-coredata-today-extension-ios8-will-they-play