Re: Question about Archives and Serialisations Programming Guide
Re: Question about Archives and Serialisations Programming Guide
- Subject: Re: Question about Archives and Serialisations Programming Guide
- From: Graham Cox <email@hidden>
- Date: Sat, 06 Feb 2016 10:24:22 +1100
> On 6 Feb 2016, at 4:00 AM, Dave <email@hidden> wrote:
>
> self.pNetworkNodeChildArray = [[NSMutableArray alloc] initWithArray:myArray copyItems:YES];
This is one reason why sometimes having explicit ivars for key parts of an object will save you many hours of trouble.
If you had e.g:
@synthesize pNetworkChildArray = mChildArray; // mChildArray is an ivar NSMutableArray*
your -initWithCoder: method could then do something simple like:
mChildArray = [[aDecoder decodeObjectForKey:@“children”] mutableCopy];
This will avoid all of the issues you’ve had and also give you better performance, because it’s not invoking a setter method, nor allocating an unneeded intermediate copy of the array. For large object graphs, (>10,000 nodes say) bypassing a property setter can end up shaving minutes off the time to decode an archive. - initWithCoder: is an init method, it should be creating the initial state of the object, incuding allocating its ivars. So you don’t have to either a) allocate the ivars then copy the decoded array into them, nor b) worry about a leak caused by simply assigning a mutable copy of a decoded array to the ivar. Another benefit of this is that you could declare your pNetworkChildArray property read-only and non-mutable in the public interface, which would catch external abuses of the network - usually only the internals of the tree itself have any business mutating this.
n.b. if you’d have shown your code rather than a long verbal description of what you were doing, the problem would have been instantly obvious. For comparison, here’s the -initWithCoder: method I have in a general purpose tree node class. It has never given the slightest trouble:
#pragma mark - NSCoding
- (instancetype) initWithCoder:(NSCoder*) aDecoder
{
self = [super init];
if( self )
{
mChildNodes = [[aDecoder decodeObjectForKey:@"Node_children"] mutableCopy];
mParentNode = [aDecoder decodeObjectForKey:@"Node_parent"];
mRepresentedObject = [[aDecoder decodeObjectForKey:@"Node_payload"] retain];
}
return self;
}
_______________________________________________
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