这是代码:
- (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] * 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'实体名称不能为零。'
我真的玩了一段时间,也无法在互联网上找到类似的东西。有趣的是,错误总是在第四次将联系人添加到不同的子视图时发生。有谁知道如何解决这个问题?我真的没有想法......
哦和调试器说:
#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
答案 0 :(得分:1)
我猜测问题是托管对象上下文已经与托管对象模型分离,这使得NSEntityDescription无法找到并返回应该代表Contact
实体的类。
最可能的原因是初始化托管对象上下文但不设置其模型或以某种方式将模型设置为nil。我建议在进行失败的调用之前完全记录托管对象上下文。特别是记录它的模型以确保它具有一个并且它从呼叫到呼叫保持相同的模型。