8

希望你能提供帮助。我正在向我的应用程序添加 Today 支持,该应用程序使用 MagicalRecord https://github.com/magicalpanda/MagicalRecord来管理我所有的 CoreData 内容。

我正在努力了解如何将我的数据呈现到 Today 扩展中。

我已经启用了此处概述的应用程序组http://blog.sam-oakley.co.uk/post/92323630293/sharing-core-data-between-app-and-extension-in-ios-8但是所有文档和我正在阅读的 StackOverflow 帖子与直接使用 CoreData 有关。MagicalRecord 为您做了很多艰苦的工作,这就是我使用它的原因,因为我在这个项目开始时对它完全陌生。所以像:

在初始化 Core Data 堆栈的地方,您将向您的 persistentStoreCoordinator 添加一个存储,如下所示:

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

只需将之前的 storeURL 值(通常在 NSDocumentDirectory 中的某个位置)更改为包含在共享 App Group 文件夹中的位置即可。你这样做使用

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

...我不明白如何/在哪里实施。

我想我只需要在我的扩展程序中设置 MagicalRecord 堆栈,就像我在我的 appDelegate 中所做的那样,但它当然失败了。

真的希望有人可能处于类似的情况,并能够阐明如何推进这一情况。

您需要我发布的任何代码都可以告诉我。

提前致谢

4

3 回答 3

5

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

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

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

[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(id)pathToStore];
于 2015-02-13T08:55:25.003 回答
4

我有同样的问题,我可以通过关注这个线程来解决它。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>];
}
于 2014-09-29T14:17:24.837 回答
3

改变

[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];
}
于 2015-02-18T19:33:12.647 回答