MagicalRecord(CoreData)+今日扩展(iOS8)…他们会玩吗?

希望你能帮忙。 我将Today支持添加到我的应用程序,该应用程序使用MagicalRecord https://github.com/magicalpanda/MagicalRecord来pipe理我所有的CoreData资料。

我正在试图理解如何将我的数据展现在Today扩展中。

我已经启用了应用程序组,如http://blog.sam-oakley.co.uk/post/92323630293/sharing-core-data-between-app-and-extension-in-ios-8但是所有的文档和我正在阅读的StackOverflowpost与直接使用CoreData有关。 MagicalRecord为你做了很多艰苦的工作,这就是为什么我使用它,因为在这个项目开始的时候,我完全不知道这些。 所以这样的事情:

在你初始化你的核心数据栈的地方,你将会添加一个商店到你的persistentStoreCoordinator中,像这样:

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

这只是一个改变你以前的storeURL值(通常NSDocumentDirectory中的某个地方)到共享应用程序组文件夹中包含的位置的问题。 你这样做使用

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

…我不了解如何/在哪里实施。

我想我只需要在我的扩展中设置MagicalRecord堆栈,就像我在我的appDelegate中那样,但是当然这是失败的。

真的希望有人可能会有类似的情况,并能够说明如何推进这一点。

任何你需要我发布的代码只是让我知道。

提前致谢

不知道这是否适用于以前版本的MagicalRecord,但从2.2版开始,您只需传递最终的url作为商店名称即可:

 NSFileManager *fileManager = [[NSFileManager alloc] init]; NSURL *directory = [fileManager containerURLForSecurityApplicationGroupIdentifier:@"group.yellow"]; NSURL *pathToStore = [directory URLByAppendingPathComponent:kMagicalRecordDefaultStoreFileName]; [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(id)pathToStore]; 

我有同样的问题,我可以解决这个问题。 https://github.com/magicalpanda/MagicalRecord/issues/858

我首先在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]]; } 

然后,在我的扩展中,我只是添加以下到其视图加载方法。

 - (void)viewDidLoad { [super viewDidLoad]; [MagicalRecord setupCoreDataStackWithStoreNamed:<storeFileName>]; } 

更改

 [MagicalRecord setupCoreDataStackWithStoreNamed:@"Database"]; 

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