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

浪子不回头ぞ 提交于 2019-12-09 16:12:09

问题


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


回答1:


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



回答2:


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



回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!