iphone - 核心数据 : Not able to add persistent store to coordinator

标签 iphone objective-c ios core-data

我正在制作一个应用程序,它根据某些属性(例如名称、颜色、重量等)显示生物体,但这些属性直到运行时才会知道。这就是我以编程方式创建 NSManagedObjectModel 的原因。看来我能够正确初始化模型,因为我使用 NSLog 语句来显示生物体实体的属性。但是当我调用该方法时:

[orgPersistentStoreCoordinator 
addPersistentStoreWithType:NSSQLiteStoreType                                        
configuration:nil URL:storUrl options:nil error:&error]

它返回“否”,我在持久存储协调器方法中收到一个错误,内容如下:

"Could not add persistent store:The operation couldn’t be completed. (Cocoa error 256.)"

我尝试打开这些选项,但仍然遇到相同的错误:

NSString * const NSMigratePersistentStoresAutomaticallyOption;
NSString * const NSInferMappingModelAutomaticallyOption;

完整代码如下:

- (NSPersistentStoreCoordinator *)orgPersistentStoreCoordinator {

if (orgPersistentStoreCoordinator != nil) {
    return orgPersistentStoreCoordinator;
}
NSURL *storUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
                                          stringByAppendingPathComponent:@"organisms.sqlite"]];
NSError *error = nil;

orgPersistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
                              initWithManagedObjectModel:[self orgManagedObjectModel]];

NSLog(@"Entities: %@",[[self orgManagedObjectModel] entities]);

if (![orgPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                                 configuration:nil URL:storUrl options:nil error:&error]) {
        /*Error for store creation should be handled in here*/
    NSLog(@"Could not add persistent store:%@",[error localizedDescription]);
}

return orgPersistentStoreCoordinator;

}

这是我如何以编程方式创建 ManagedObjectModel 的片段。

+ (NSManagedObjectModel *) orgManagedObjectModel 
{
FieldGuide *f = [FieldGuide sharedFieldGuide];



NSManagedObjectModel *orgModel = [[NSManagedObjectModel alloc]init];

NSEntityDescription *organismEntity = [[NSEntityDescription alloc]init];
[organismEntity setName:@"OrganismCoreData"];
[organismEntity setManagedObjectClassName:@"OrganismCoreData"];
[orgModel setEntities:[NSArray arrayWithObject:organismEntity]];


NSMutableArray *orgEntityProperties = [[NSMutableArray alloc]init];

 // 1. Name attributes
for (NameParameter *n in f.nameAttributes) 
{
    NSAttributeDescription *nameAttr = [[NSAttributeDescription alloc]init];
    [nameAttr setName:n.name];
    [nameAttr setAttributeType:NSStringAttributeType];
    [orgEntityProperties addObject:nameAttr];
}
[organismEntity setProperties:orgEntityProperties];


return orgModel;
}

最后,当我尝试插入一些生物并通过 NSManagedObjectContext 保存时,应用程序终止并在日志中显示此内容:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'

任何帮助或指导将不胜感激,谢谢!

编辑: 是的,我尝试从模拟器中删除应用程序,清理目标,并通过查找器从文档文件夹中删除organism.sqlite。

编辑#2: 我忘了补充一点,addPersistentStore 方法还会返回另一个错误:

2012-04-30 14:13:46.109 biodex[7510:14803] CoreData: error: (1) I/O error for database at /Users/junryandelacruz/Library/Application Support/iPhone Simulator/5.1/Applications/ACE957AF-2FC0-4EAF-B226-5019F70FAB90/Documents/organisms.sqlite.  SQLite error code:1, 'near "OR": syntax error'

最佳答案

您的storeURL 中似乎有错误。这是“未知文件读取错误”,通常与文件路径有关。

来自documentation :

NSFileReadUnknownError = 256,

第二个错误当然与添加持久存储无关。检查代码中的“OR”谓词。

关于iphone - 核心数据 : Not able to add persistent store to coordinator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10390358/

相关文章:

ios - 保持 UIStackView 位于比例 UIStackView 内的正下方、顶部 UIStackView

iphone - iOS App无法在后台运行

iphone - CGAffineTransform(旋转)后 view.frame.size 属性会发生什么?

ios - 在 Swift 中使用 value 参数时 genstrings 会阻塞

objective-c - Objective C 中的嵌入式资源?

ios - setNeedsDisplay 破坏了 UIView 的自动布局动画

ios - 将属性字符串附加到 UITextField 的属性文本并保留其所有以前的颜色和属性

ios - NSNotification(或类似的)来检测 iOS 上蓝牙连接的变化?

iphone - Xcode 对旧目标上不可用的新方法显示警告?

iphone - 方法调用乱序?