Search code examples
cocoacore-datansmanagedobjectnsarraycontrollernsoutlineview

Custom NSMangedObject accessor crashed NSOutlineView


I am thinking this is a bug in Core Data but before I file a bug report, I want to be sure it is not just me being stupid.

I set up an NSOutlineView to access the data of 5 different Core Data entities. Each entity's data is accessed with a different NSArrayController which is bound to the Entity and its' ManagedObjectContext. I then have the NSOutlineViewDataSource methods return the correct NSString object depending on which entity was expanded.

NOTE: entities is declared elsewhere as an NSArray with names for the entities.

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index
       ofItem:(id)item {

if(nil == item) {
    return [entities objectAtIndex:index];
}
NSInteger entityIdx = [entities indexOfObject:item];
if (entityIdx == NSNotFound) {
    return @"";
}
id returnObject = @"";
switch (entityIdx) {
    case 0: {
        Person *person  = [[peopleArrayController arrangedObjects] objectAtIndex:index];
        returnObject = person.fullName;
        break;
    }
    case 1: {
        Media *media = [[mediaArrayController arrangedObjects] objectAtIndex:index];
        returnObject = media.imageTitle;
        break;
    }
    case 2: {
        Note *note  = [[notesArrayController arrangedObjects] objectAtIndex:index];
        returnObject = note.noteDescription;
        break;
    }
    case 3: {
        Source *source = [[sourcesArrayController arrangedObjects] objectAtIndex:index];
        returnObject = source.title;
        break;
    }
    case 4: {
        Repository *repo = [[repostioriesArrayController arrangedObjects] objectAtIndex:index];
        returnObject = repo.name;
        break;
    }
    default:
        break;
}


return returnObject;

}

The Person entity property fullName and the Media entity property imageTitle are custom accessors.

- (NSString *)fullName {
[self willAccessValueForKey:@"surName"];
[self willAccessValueForKey:@"givenName"];
NSString *firstName = [self valueForKey:@"givenName"];
NSString *lastName = [self valueForKey:@"surName"];
NSString *string = [NSString stringWithFormat:@"%@ %@", (firstName) ? firstName : @"", (lastName) ? lastName : @""];
[self didAccessValueForKey:@"surName"];
[self didAccessValueForKey:@"givenName"];
return string;

}

- (id) imageTitle {
[self willAccessValueForKey:@"path"];
id title = [[self valueForKey:@"path"] lastPathComponent];
[self didAccessValueForKey:@"path"];
return title;

}

The program was crashing when I tried to expand the Person or the Media entities but not when I expanded the other entities. I traced the crash to [NSCell _setContents:][NSObject(NSObject) doesNotRecognizeSelector:]

I changed the Media property being returned to a standard Core Data accessor property @"path" and the program stopped crashing when I expanded the Media entity. So the problem is definitely related to the custom accessor.

FYI - I checked to make sure the entity was set to use the NSManagedObject class.

Can anyone give me a reason for the crash other than a bug?


Solution

  • I got around this problem by changing the method

    - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item

    to return the managed object

    case 1: {
            Media *media = [[mediaArrayController arrangedObjects] objectAtIndex:index];
            returnObject = media;
            break;
        }
    

    Then I have the method - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item

    return the String

        if ([item isKindOfClass:[Media  class]]) {
        Media *media = (Media *)item;
        return media.imageTitle;
    }