CoreData not noticing changes to transformable properties
CoreData not noticing changes to transformable properties
- Subject: CoreData not noticing changes to transformable properties
- From: Jonathan del Strother <email@hidden>
- Date: Fri, 3 Dec 2010 18:49:30 +0000
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