Re: NSCoder with network
Re: NSCoder with network
- Subject: Re: NSCoder with network
- From: Graham Cox <email@hidden>
- Date: Mon, 14 Dec 2009 10:02:13 +1100
On 14/12/2009, at 4:29 AM, James Maxwell wrote:
> I've got a potentially tricky problem here. My app is backed by a network-like data structure based on three objects: a Network_Controller, a Network, and a set of Nodes. The network is arranged in a hierarchy of levels, with each level having at least one node. The Network object stores this as a 2D NSArray; each level is an NSArray, which holds an NSArray of Nodes for the given level. The Nodes, obviously, also have parent/child relationships.
> Now, the problem has been in reading files saved using NSCoder. I'm reading/writing these files in a fairly typical manner, at this stage, by having the Network_Controller initiate the save, which tells the Network to encode its 2D array of Nodes, each of which encodes its necessary data, in turn (as per the typical way of using encodeWithCoder/initWithCoder). A single Node's data, however, includes references to parent/child Nodes. So the question is, how to best decode this from the file? Ideally, I suppose, it should start at the top level, decode the single top-level Node first, then work its way down. I'm not doing that, at this point, as I'm just letting the Network decode its 2D array of Nodes, and crossing my fingers. It loads, but the behaviour of the whole thing after loading is not what it should be - it's a complex system, so it's hard to identify exactly why, but it isn't doing what it should be doing.
> Any thoughts on how to manage this sort of decoding? And should I also approach the encoding in a particular way?
Hi James,
You don't necessarily have to do anything special. Just encode & decode the parent as normal. Naturally you don't want to retain the parent - that would be a retain cycle.
But you might also want to consider what would happen if you wanted to archive a node independently of the rest of the network (perhaps for cut/paste or something else). In that case you wouldn't want to drag in the entire network when archiving, as it would if you just encoded the parent unconditionally. In that case you would want to use -encodeConditionalObject:forKey: which encodes the parent only if it has already been encoded.
That then leaves the final problem of dearchiving an independently archived node and inserting it back into a network. That's usually just a case of having the parent pass itself to a setParent: method of the node when you add the node. But if there's also a chance of setting a whole bunch of nodes at once, then the -makeObjectsPerformSelector: withObject: method is an easy way to call -setParent: on all the inserted nodes at once.
So a complete tree archiving/dearchiving solution typically combines all of these.
--Graham
_______________________________________________
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