Re: CoreData not noticing changes to transformable properties
Re: CoreData not noticing changes to transformable properties
- Subject: Re: CoreData not noticing changes to transformable properties
- From: Adam Swift <email@hidden>
- Date: Fri, 3 Dec 2010 11:03:28 -0800
Core data attributes are expected to be immutable and will be treated as such. To property track changes to the value of an attribute you'll need to replace the attribute object on the owning managed object with a different instance.
Take a look at the encapsulation section of the model object design consideration docs for more info:
http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ModelObjects/Articles/moBasics.html#//apple_ref/doc/uid/TP40002134-SW3
- adam
On Dec 3, 2010, at 10:49 AM, Jonathan del Strother wrote:
> Hi,
>
> I have an NSManagedObject with a transformable property
> arrayOfWidgets, which is an NSArray of Widget instances, which
> implement encodeWithCoder:
>
> If I treat the Widget class as immutable, it all works fine : I assign
> an NSArray of initialized widgets to arrayOfWidgets, and call save on
> the managed object context. -[Widget encodeWithCoder:] gets called,
> my widgets are all persisted and load successfully the next time I
> launch my app.
>
> However, if I change a property of a widget after it's been saved,
> it's extremely difficult to get CoreData to notice the change. For
> example (pseudocode):
>
> self.arrayOfWidgets = [NSArray arrayWithObject:[Widget widgetWithName:@"Alf"]];
> [self.managedObjectContext save:NULL];
> // Successful save, arrayOfWidgets has 1 element with name Alf.
>
> [[self.arrayOfWidgets lastObject] setName:@"Bob"];
> [self.managedObjectContext save:NULL];
> // CoreData doesn't notice this change, not entirely unexpectedly -
> the persisted name is still Alf.
>
> [[self.arrayOfWidgets lastObject] setName:@"Bob"];
> self.arrayOfWidgets = [NSArray arrayWithObject:[self.arrayOfWidgets
> lastObject]];
> [self.managedObjectContext save:NULL];
> // CoreData doesn't even notice this change, which seems bizarre given
> I've assigned an entire new array to arrayOfWidgets. Persisted name
> is still Alf.
>
> self.arrayOfWidgets = [NSArray arrayWithObject:[Widget widgetWithName:@"Bob"]];
> [self.managedObjectContext save:NULL];
> // CoreData finally notices the change - I have to basically
> deep-clone the array, creating new instances all around, before the
> persisted name changes to Bob.
>
>
> Does that make sense? Is there any way of sensibly dealing with
> mutable objects in transformable CoreData attributes?
>
> -Jonathan
> _______________________________________________
>
> 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
_______________________________________________
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