开发者

ios - core data: Why does "Entity name must not be nil" sometimes occur?

hey folks. i am writing an ios-app, that contains severals subviews, which can be individually created by a touch of a button. every subview has an instance of 'group', that is an entity saved via core data. 'group' is in to-many relationship with 'contact'. when a contact is dragged onto a subview, it is saved in core data for the given 'group'. this works fine exactly 3 times. on every 4th time a contact is being dragged onto another subview the app crashes.

here is the code:

- (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*)[NSE开发者_Go百科ntityDescription 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];
    }

}}

when trying to add a contact to the group the error says:

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

i really played around a while and also couldnt find anything similar on the internet. it's funny the error always occurs the fourth time a contact is added to a different subview. anyone any idea on how i can fix this? i am really out of ideas...

oh and debugger says:

#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


I am going to guess that the problem is that the managed object context has become separated from the managed object model which makes it impossible for NSEntityDescription to find and return the class that should represent the Contact entity.

The most likely cause of this would be initializing a managed object context but not setting its model or somehow setting the model to nil. I would suggest fully logging the managed object context before making the call that fails. In particular log its model to make sure it has one and that it keeps the same one from call to call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜