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

夙愿已清 提交于 2019-12-04 03:15:13

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