On Aug 9, 2012, at 22:49 , Martin Hewitson <martin.hewit...@aei.mpg.de> 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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to