views:

57

answers:

1

Hello,

I have a NSManagedObject with a NSMutableArray as attribute:

@interface MyObject :  NSManagedObject  
{
}

@property (nonatomic, retain) id a1;

In the data model it is declared as Transformable. I left the Value Transformer field like it is with the default (in light grey) NSKeyedUnarchiveFromData.

a1 is created as part of theObject:

MyObject  *theObject = [NSEntityDescription insertNewObjectForEntityForName:@"MyObject" inManagedObjectContext: myManagedObjectContext];

and initialized:

a1 = [[NSMutableArray alloc] init];

Objects are added to a1 with [a1 insertObject:[NSNumber numberWithInt:0] atIndex: 0 ];

Then I save the context after all that. Loading the context back all elements stored in a1 are saved and loaded. ALL WORKS WELL!

BUT when now a1 CHANGES, for example by adding one more element to a1 or changing any element within a1 and the context is being saved and loaded back, the content of a1 is UNCHANGED (it remains exactly as it was before all changes happened). CHANGES DON'T WORK!

By the way, while the app is running, all changes to a1 ARE STORED in a1.

Please, can you help - what is going on here?

Thanks ver much for your help!

+3  A: 

Changes inside of your Array are not going to work because Core Data cannot see into the array.

The short answer is don't do this. This is no reason ever to store an array (or dictionary for that matter) in Core Data.

Create a new entity in Core Data and create a relationship. If the order is important, put an order attribute in the child table.

Do not store arrays as binary objects.

Marcus S. Zarra
...in the meanwhile I tried a couple of things, like recreating a brand new array, replacing the old one with the new one and stuff like that ... and!! I can absolutely underline your recommendation: DON'T DO THIS.So I ended up doing exactly what you said - however - this seems rather inefficient in cases where there are a couple of number arrays - representing all these with sets (where all the objects need to have an artificial sort-attribute)is really not a good approach. I ask myself, how could all this survive for such a long time - there are many others who need to store arrays.Thanks!
It is not inefficient since you need to turn those numbers into objects to store them into an array anyway. In addition, when you consider the additional benefits of being able to search on those numbers, filter on those numbers, etc. The minor cost of a relationship is trivial compared to the benefits. The cost of an entity compared to storing an array or dictionary is so minor as to be equal to zero.
Marcus S. Zarra
+1 Sometime the answer to the question, "How do I do that," is "Don't do that."
TechZen