Implementing NSOutlineView autosaveExpandedItems with CoreData
Implementing NSOutlineView autosaveExpandedItems with CoreData
- Subject: Implementing NSOutlineView autosaveExpandedItems with CoreData
- From: Ian <email@hidden>
- Date: Tue, 1 Jul 2008 00:07:31 +0100
Hi all,
I've got a CoreData object graph with an outline view showing
instances of an NSManagedObject subclass called "Group".
The Group class has the standard to-one relation 'parent' and to-many
'children'.
The outline view is working perfectly.
I've set autosaveExpandedItems to YES and when I expand or unexpand
(contract?) a group my outline view's data source gets sent the
persistentObjectForItem message.
I can see in the prefs plist for my app that the expanded Group itmes
are being saved, and when my application launches, the outline view's
data source object gets sent the itemForPersistentObject message.
Below is what I'm doing for each of these messages:
- (id)outlineView:(NSOutlineView*)outlineView persistentObjectForItem:
(id)item
{
NSNumber *uid=[[item representedObject] valueForKeyPath:@"uid"];
id archivedObject=[NSKeyedArchiver archivedDataWithRootObject:uid];
return archivedObject;
}
- (id)outlineView:(NSOutlineView *)outlineView itemForPersistentObject:
(id)object
{
NSNumber *uid=[NSKeyedUnarchiver unarchiveObjectWithData:object];
NSManagedObjectContext *context=[self managedObjectContext];
NSFetchRequest *request=[[NSFetchRequest alloc] init];
NSEntityDescription *groupEntityDescription=[NSEntityDescription
entityForName:@"Group" inManagedObjectContext:context];
[request setEntity:groupEntityDescription];
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"uid==
%@",uid];
[request setPredicate:predicate];
NSError *error;
NSArray *fetchResults=[context executeFetchRequest:request
error:&error];
if ([fetchResults count])
return [fetchResults objectAtIndex:0];
return nil;
}
The persistentObjectForItem method successfully archives and returns
the 'uid' property for the item's represented object - this is a
unique identifier for each of my Groups.
The itemForPersistentObject successfully reconstitutes the archived
uid and correctly finds the Group with the unique 'uid' property and
returns it.
Expanded items in the outline view are not being restored however.
Is itemForPersistentObject expecting me to return something else?
My guess would be that it was expecting an instance of NSTreeNode,
since this is what I first got as 'item' in persistentObjectForItem.
Sadly though, I can't see a way to get an NSTreeNode from an object.
I have [item representedObject] for archival, but not [object
representingItem], if you see what I mean.
Obviously I've tried returning an the 'item' archived but it doesn't
respond to archiving.
Neither (obviously) does my NSManagedObject subclass... should I
implement archiving in my subclass to get this to work?
Does anyone have any clue they can distribute my way? (citation: http://www.bofhcam.org/co-larters/distributing-clue/index.html)
I've seen lots in the usual places (lists.apple.com, cocoabuilder.com
et al) about this but no actual solutions shout out at me. Perhaps I'm
not looking hard enough. (most likely).
Many TIA
Ian
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden