Emi*_*ett 8 core-data objective-c ios ios8 today-extension
希望你能提供帮助.我正在为我的应用添加今日支持,它使用MagicalRecord https://github.com/magicalpanda/MagicalRecord来管理我的所有CoreData内容.
我正在试图理解如何将我的数据显示到今天的扩展中.
我已启用了此处列出的应用群组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]
Run Code Online (Sandbox Code Playgroud)
只需将storeURL的先前值(通常位于NSDocumentDirectory中的某个位置)更改为共享App Group文件夹中包含的位置即可.你这样做
containerURLForSecurityApplicationGroupIdentifier: NSURL *directory =
[[NSFileManager defaultManager]
containerURLForSecurityApplicationGroupIdentifier:@"group.YourGroupName"];
NSURL *storeURL = [directory
URLByAppendingPathComponent:@"YourAppName.sqlite"];
Run Code Online (Sandbox Code Playgroud)
......我不明白如何/在哪里实施.
我想象我必须像我在appDelegate中那样在我的扩展中设置MagicalRecord堆栈,但当然它失败了.
真的希望有人可能处于类似的情况,并能够阐明如何推进这个问题.
你需要我发布的任何代码让我知道.
提前致谢
不确定这是否适用于以前版本的MagicalRecord,但从2.2开始,你可以将最终的url作为商店名称传递:
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL *directory = [fileManager containerURLForSecurityApplicationGroupIdentifier:@"group.yellow"];
NSURL *pathToStore = [directory URLByAppendingPathComponent:kMagicalRecordDefaultStoreFileName];
[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(id)pathToStore];
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,我可以通过遵循此线程来解决它。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]];
}
Run Code Online (Sandbox Code Playgroud)
然后在我的扩展中,我刚刚将以下内容添加到其视图确实加载方法中。
- (void)viewDidLoad {
[super viewDidLoad];
[MagicalRecord setupCoreDataStackWithStoreNamed:<storeFileName>];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2176 次 |
| 最近记录: |