2
votes

I'm having a hard time with this. I am getting a very odd behavior when it comes to object persistence of a view based NSOutlineView. Currently I have an object that contains an array of object1 that contains an array of object2 that contains an array of object3, etc... Each each class (object) has an isEqual: method and an isEqualToObject: method. I have revised these over and over again, they work fine.

Now when I expand an item, it is saved to the defaults plist and I have checked it, the number of items saved is correct.

What happens though is that when I open my document again; sometimes all the rows are expanded exactly as I left them, sometimes none of the rows are expanded and sometimes NSOutlineView will expand some rows but not others.

To note I am not using NSTreeController. I am using NSOutlineView DataSource and Delegate. The following methods are implemented like this. Also the AutoSave name for the outline view is set.

- (id)outlineView:(NSOutlineView *)outlineView persistentObjectForItem:(id)item {
        return [NSKeyedArchiver archivedDataWithRootObject:item];
    }

- (id)outlineView:(NSOutlineView *)outlineView itemForPersistentObject:(id)object {
        return [NSKeyedUnarchiver unarchiveObjectWithData:object];

}

Basically sometimes it works, sometimes it half works and sometimes not at all. It is very random.

The odd thing is that I've placed NSLog and each item is finding a match but... the outline view is not expanding it.

1
i think you should use [item representedObject] to get the underlying object aka as in your model.Volker
additionally, when checking my code i found that I call in awakeFromNibof my document class [self.groupView setAutosaveExpandedItems:YES];. I remember reading that you have to call it manually. I assume you also have set the autosave name?Volker
I already have the autosave name set. [item representedObject] only works if I'm using NSTreeController which I am not using.MAH

1 Answers

1
votes

I got it to work. So what I ended up doing is searching through each array I have for the object and return the matching object (not the persistentObject but the object that is part of my data already). In other words

- (id)outlineView:(NSOutlineView *)outlineView itemForPersistentObject:(id)object {

    id persistentObject =[NSKeyedUnarchiver unarchiveObjectWithData:object];

    for (Class * a in dataArray) {
        if ([a isEqual:persistentObject]) {
            return a;  //instead of returning persistentObject
        }
    }
}

This did the trick. I've searched long and wide for this and I hope someone finds it useful.