ios核心数据:为什么;实体名称不得为“零”;有时会发生什么?

ios核心数据:为什么;实体名称不得为“零”;有时会发生什么?,ios,core-data,nsentitydescription,Ios,Core Data,Nsentitydescription,嘿,伙计们。我正在编写一个ios应用程序,它包含几个子视图,只需按一下按钮即可单独创建。每个子视图都有一个“组”实例,即通过核心数据保存的实体“组”与“联系人”之间存在很多关系。将联系人拖动到子视图时,它将保存在给定“组”的核心数据中。这可以正常工作3次。每四次将联系人拖到另一个子视图时,应用程序就会崩溃 代码如下: - (void)fetchContacts { if(personRecordIDsArray==nil) { personRecordIDsArray = [[NSMut

嘿,伙计们。我正在编写一个ios应用程序,它包含几个子视图,只需按一下按钮即可单独创建。每个子视图都有一个“组”实例,即通过核心数据保存的实体“组”与“联系人”之间存在很多关系。将联系人拖动到子视图时,它将保存在给定“组”的核心数据中。这可以正常工作3次。每四次将联系人拖到另一个子视图时,应用程序就会崩溃

代码如下:

- (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];
    }

}}

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


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

奇怪!您能否尝试不强制转换(Contact*)到
insertNewEntityForName
方法?我以前从未见过它是这样构造的,即使它有点道理,你也不需要它。看看有没有什么不同?
#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