[aerogear-dev] Mutable vs Immutable for AGMemoryStore and AGPropertyStore

Erik Jan de Wit edewit at redhat.com
Mon Nov 25 08:50:29 EST 2013


Hi,

I would go for changing the API to immutable return values, because shared state is the mother of all bug ;)

Cheers,
	Erik Jan

On 25 Nov,2013, at 14:24 , Corinne Krych <corinnekrych at gmail.com> wrote:

> Hello Guys,
> 
> Using AGPropertyListStorage in Xmas app [1], I run into this interesting issue:
> 
> In the app I retrieve a NSArray of NSMutableDictionary, I get a mutable copy of the NSArray and I save encrypted data in it.
> I decrypt some of the data in memory BUT I actually don't want those to be saved decrypted into permanent storage.
> And then let's say I add some more data with encrypted value then I save this dato to my AGProperListStorage. Just this one.
> But surprise, if I look in my permenet storage, I see my newly data encrypted but the other data I ecrypted in memory were saved decrypted.
> 
> What happened?
> In one word: shallow mutable copy of NSArray.
> My mutable array still point to original MSMutableDictionary and when saving one data to permanent storage the all memory is dumped into plist resulting in decrypted value to be stored.
> 
> How did I fixed it?
> Using deep mutable copy see in [2].
> 
> In AGMemoryStorage (and its inherited AGPropertyListStorage) we use NSMutableDictionary because you can save item (represented as Key/Value with NSMutableDictionary) without id, the store will take care of generating this id and assign it back into the item. 
> 
> Even the AGMemoryStorage which returns a NSArray of NSMutableDictionary is misleading because you can actually do 
> NSArray myArray = [store reallAll]
> and then modify the contain of the memory storage without using save.
> 
> That experience brings the topic whether it's a good pratice to use mutable object 
> 
> Options
> - option1: stick to immutable objects whithin the AGStore and let the save method return an immutable NSDictinary containing the item
> - option2: leave it as it is, responsability of user to do deep copy
> - others ?
> 
> Personal Preference go for option1 which I found less confusing.
> 
> ++
> Corinne
> [1] https://github.com/corinnekrych/aerogear-ios-cookbook/blob/master/Xmas/Xmas.md
> [2] https://github.com/corinnekrych/aerogear-ios-cookbook/blob/master/Xmas/Xmas/Classes/Controller/AGGiftListCollectionViewController.m#L66
> _______________________________________________
> aerogear-dev mailing list
> aerogear-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/aerogear-dev




More information about the aerogear-dev mailing list