在 TodayExtension 中使用 MagicalRecord (CoreData)
Use MagicalRecord (CoreData) inside TodayExtension
我正在为任务列表的生产力应用程序开发 TodayExtension,我需要在 TodayExtension 内的 table 中显示一些任务的列表。
我在设置 table 视图时没有任何问题,但在尝试设置 MagicalRecord 和 CoreData 时出现崩溃。
我的崩溃方法的源代码是:
- (void)setupCoreDataStackWithStoreNamed:(NSString *)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).'
我从中得到了灵感postHow 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];
我正在为任务列表的生产力应用程序开发 TodayExtension,我需要在 TodayExtension 内的 table 中显示一些任务的列表。
我在设置 table 视图时没有任何问题,但在尝试设置 MagicalRecord 和 CoreData 时出现崩溃。
我的崩溃方法的源代码是:
- (void)setupCoreDataStackWithStoreNamed:(NSString *)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).'
我从中得到了灵感postHow 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];