MagicalRecord 在 ios 8.1 上首次运行时崩溃
MagicalRecord crashes on first runtime on ios 8.1
我使用 MagicalRecord 在我的项目中添加了 CoreData,但它在这个方法上崩溃了:
+(NSString *) MR_entityName {
NSString *entityName;
if ([self respondsToSelector:@selector(entityName)])
{
entityName = [self performSelector:@selector(entityName)];
}
if ([entityName length] == 0)
{
// Remove module prefix from Swift subclasses
entityName = [NSStringFromClass(self) componentsSeparatedByString:@"."].lastObject;
}
return entityName;
}
在第一个 if 上,在控制台中我的 entityName 是 nil。
有人可以帮我吗? @Edit:我添加了一些代码,这就是我现在在控制台中拥有的代码:
TestCoreData[483:6632] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/macbookpro/Library/Developer/CoreSimulator/Devices/0E01E4C3-42E3-44A1-BE57-2A535715EE03/data/Containers/Data/Application/586065AE-FF44-4396-8999-9E06391595F6/Documents/ options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x7be94450 {NSUnderlyingException=unable to open database file, NSSQLiteErrorDomain=14} with userInfo dictionary {
NSSQLiteErrorDomain = 14;
NSUnderlyingException = "unable to open database file";
}
该错误发生得更早,而不是在此方法期间,但您在错误发生时忽略了错误,因此您稍后会崩溃。
该错误具体描述了调用 addPersistentStoreWithType:configuration:URL:options:error:
时出现的问题,该方法是 NSPersistentStoreCoordinator
上的方法。不幸的是,Cocoa error 256
部分含糊不清,但 NSSQLiteErrorDomain
指出了持久存储文件的问题。
出现该问题是因为 URL 您传入的指向应用程序的文档目录。您需要将路径传递给文件名。该文件不必存在,如果需要将创建它。但它必须是文件的路径,因为 Core Data 不会为您创建文件名。
这意味着您将 [self applicationDocumentsDirectory]
作为 URL 传递。它需要是该目录中的一个文件,类似于 [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyPersistentStore.sqlite"]
.
我使用 MagicalRecord 在我的项目中添加了 CoreData,但它在这个方法上崩溃了:
+(NSString *) MR_entityName {
NSString *entityName;
if ([self respondsToSelector:@selector(entityName)])
{
entityName = [self performSelector:@selector(entityName)];
}
if ([entityName length] == 0)
{
// Remove module prefix from Swift subclasses
entityName = [NSStringFromClass(self) componentsSeparatedByString:@"."].lastObject;
}
return entityName;
}
在第一个 if 上,在控制台中我的 entityName 是 nil。 有人可以帮我吗? @Edit:我添加了一些代码,这就是我现在在控制台中拥有的代码:
TestCoreData[483:6632] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/macbookpro/Library/Developer/CoreSimulator/Devices/0E01E4C3-42E3-44A1-BE57-2A535715EE03/data/Containers/Data/Application/586065AE-FF44-4396-8999-9E06391595F6/Documents/ options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x7be94450 {NSUnderlyingException=unable to open database file, NSSQLiteErrorDomain=14} with userInfo dictionary {
NSSQLiteErrorDomain = 14;
NSUnderlyingException = "unable to open database file";
}
该错误发生得更早,而不是在此方法期间,但您在错误发生时忽略了错误,因此您稍后会崩溃。
该错误具体描述了调用 addPersistentStoreWithType:configuration:URL:options:error:
时出现的问题,该方法是 NSPersistentStoreCoordinator
上的方法。不幸的是,Cocoa error 256
部分含糊不清,但 NSSQLiteErrorDomain
指出了持久存储文件的问题。
出现该问题是因为 URL 您传入的指向应用程序的文档目录。您需要将路径传递给文件名。该文件不必存在,如果需要将创建它。但它必须是文件的路径,因为 Core Data 不会为您创建文件名。
这意味着您将 [self applicationDocumentsDirectory]
作为 URL 传递。它需要是该目录中的一个文件,类似于 [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyPersistentStore.sqlite"]
.