ios - 核心数据 : Why does "Entity name must not be nil" sometimes occur?

标签 ios core-data nsentitydescription

嘿伙计们。我正在编写一个 ios 应用程序,其中包含多个 subview ,可以通过触摸按钮单独创建这些 subview 。每个 subview 都有一个“组”实例,它是通过核心数据保存的实体。 “组”与“联系人”是一对多关系。当联系人被拖到 subview 上时,它将保存在给定“组”的核心数据中。这正好可以正常工作 3 次。每 4 次将联系人拖到另一个 subview 上时,应用程序就会崩溃。

这是代码:

- (void)fetchContacts {
if(personRecordIDsArray==nil) {
    personRecordIDsArray = [[NSMutableArray alloc] init];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];
    [entity release];

    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[[managedObjectContext executeFetchRequest:request error:&error] mutableCopy] autorelease];
    if (mutableFetchResults == nil) {
        // Handle the error.
    }
    [error release];
    [request release];
    for (Contact *contact in mutableFetchResults) {
        if(contact.belongsToGroup == group) {
            [personRecordIDsArray addObject:contact.recordId];
        }
    }
}   
NSLog(@"%d", [personRecordIDsArray count]);}

- (void)addContactsToGroup:(NSArray*)arrayOfPeople {
[self fetchContacts];
for(int i = 0; i<[arrayOfPeople count]; i++) {
    ABRecordRef person = [arrayOfPeople objectAtIndex:i];
    NSNumber *personRecordId = [NSNumber numberWithInteger:ABRecordGetRecordID(person)];
    if(![self groupContainsContactWithRecordID:personRecordId]) {
        NSString *compositeName = (NSString *)ABRecordCopyCompositeName(person);
//Here is when the error occurs:
        Contact *contact = (Contact*)[NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:managedObjectContext];
        contact.compositeName = compositeName;
        contact.recordId = personRecordId;
        contact.belongsToGroup = group;
        NSError *error = nil;
        if (![managedObjectContext save:&error]) {
            NSLog(@"Error in addContactsToGroup!");}
        [error release];    
        [personRecordIDsArray addObject:personRecordId];
    }

}}

尝试将联系人添加到群组时,出现错误:

2011-04-10 16:16:36.152 TestApp[796:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Entity name must not be nil.'

我真的玩了一段时间,在互联网上也找不到类似的东西。有趣的是,第四次将联系人添加到不同的 subview 时总是会发生错误。有人知道我该如何解决这个问题吗?我真的没主意了...

哦,调试器说:

#0  0x956e2156 in __kill
#1  0x956e2148 in kill$UNIX2003
#2  0x95774899 in raise
#3  0x9578a9b8 in abort
#4  0x91de4fda in __gnu_cxx::__verbose_terminate_handler
#5  0x0141c4e7 in _objc_terminate
#6  0x91de317a in __cxxabiv1::__terminate
#7  0x91de31ba in std::terminate
#8  0x91de32b8 in __cxa_throw
#9  0x0141c635 in objc_exception_throw
#10 0x00ce1486 in +[NSManagedObject(_PFDynamicAccessorsAndPropertySupport) classForEntity:]
#11 0x00ce11f6 in _PFFastEntityClass
#12 0x00d06e73 in +[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:]
#13 0x00009a02 in -[GroupViewController addContactsToGroup:] at GroupViewController.m:152
#14 0x00002f31 in -[GrouperViewController dragingWillEnd:forContacts:atPosition:] at GrouperViewController.m:98
#15 0x0000cbb8 in -[ContactsTableViewController longPressOnView:] at ContactsTableViewController.m:65

最佳答案

我猜测问题在于托管对象上下文已与托管对象模型分离,这使得 NSEntityDescription 无法找到并返回应表示 Contact 实体的类.

最可能的原因是初始化托管对象上下文,但未设置其模型或以某种方式将模型设置为零。我建议在进行失败的调用之前完全记录托管对象上下文。特别是记录其模型,以确保它有一个模型,并且每次调用时都保持相同的模型。

关于ios - 核心数据 : Why does "Entity name must not be nil" sometimes occur?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5612476/

相关文章:

ios - NSManagedObjectContext 只工作了几次,然后就停止工作了

swift - 如何在 Swift 中向实体插入新数据?

ios - 如何将私钥添加到分发证书中?

ios - 如何防止tableview section head在滚动时粘住?

ios - 核心数据托管上下文未保存在多线程环境中

ios - 尝试重新排序 NSMutableArray 时出现运行时异常

iphone - 创建自定义 URL 方案操作

ios - 从应用委托(delegate)获取当前 View Controller

ios - NSManagedObjectModel mergedModelFromBundles错误