我正在为任务列表的生产力应用程序开发 TodayExtension,我需要在 TodayExtension 内的表格中显示一些任务的列表。
我在设置表格 View 时没有任何问题,但是当我尝试设置 MagicalRecord 和 CoreData 时出现崩溃。
我的崩溃方法的源代码是:
- (void)setupCoreDataStackWithStoreNamedNSString *)storeNamed
{
if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil)
{
return;
}
NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
NSString *containerPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP].path;
NSString *sqlitePath = [NSString stringWithFormat"%@/%@", containerPath, storeNamed];
NSURL *storeURL = [NSURL URLWithString:sqlitePath];
[psc MR_addSqliteStoreNamed:storeURL withOptions:nil];
[NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:psc];
[NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:psc];
}
崩溃的那一行是:
[psc MR_addSqliteStoreNamed:storeURL withOptions:nil];
错误信息是:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'CoreData SQL stores only support file URLs (got /private/var/mobile/Containers/Shared/AppGroup/66A40F3A-9E25-42BD-9BD0-D86BC5A93FBD/myappname_560967e1665e88ab37000000.sqlite).'
我的灵感来自这篇文章 How to access CoreData model in today extension (iOS)
您正在将 NSString
url (/../...) 传递给 MR_addSqliteStoreNAamed
,但崩溃告诉您需要传递文件 url (文件://...)
所以你必须改变这个源代码:
NSString *containerPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP].path;
NSString *sqlitePath = [NSString stringWithFormat"%@/%@", containerPath, storeNamed];
NSURL *storeURL = [NSURL URLWithString:sqlitePath];
到这里:
NSURL *storeURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP];
storeURL = [storeURL URLByAppendingPathComponent:storeNamed];
另一种解决方案是:
NSString *pathToYourSqliteFile = @"/your/path/here";
NSURL *storeURL = [NSURL fileURLWithPath: isDirectory:NO];
关于ios - 在 TodayExtension 中使用 MagicalRecord (CoreData),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35102335/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |