Re: NSUserDefaults locks up on 10.8
Re: NSUserDefaults locks up on 10.8
- Subject: Re: NSUserDefaults locks up on 10.8
- From: Quincey Morris <email@hidden>
- Date: Thu, 09 Aug 2012 23:15:21 -0700
On Aug 9, 2012, at 22:49 , Martin Hewitson <email@hidden> wrote:
> I have a table view bound to shared user defaults with a key which returns an array of strings.
> I have a button for creating a new entry. This calls a piece of code like this:
>
>
> NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
> NSMutableArray *commands = [defaults mutableArrayValueForKey:TECiteCommands];
> [commands addObject:newCommand];
> [defaults setObject:commands forKey:TECiteCommands];
> [defaults synchronize];
> [commandsTable reloadData];
> I guess I'm abusing the use of NSUserDefaults somehow, but I don't know how.
I've seen an array operation hang before, when 'mutableArrayValueForKey:' is not being used properly.
It's improper because NSUserDefaults isn't MVC-compliant this for kind of change. A setting that's an array object (in spite of the convenience accessor 'arrayValueForKey:') isn't represented by an array property at all. (It is represented, I would imagine, by a NSData object that consists of the results of archiving the array object you originally specified.)
You should probably do something like this instead:
> NSMutableArray *commands = [[defaults arrayValueForKey:TECiteCommands] mutableCopy]; // because arrayValueForKey returns an immutable object
> [commands addObject:newCommand];
> [defaults setObject:commands forKey:TECiteCommands];
My guess is that it only ever worked because of an accidental implementation detail.
_______________________________________________
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