Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableArray stored with core data = WORKS, but after changing array DOESN'T WORK

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!

like image 866
user387184 Avatar asked Aug 06 '10 18:08

user387184


1 Answers

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.

like image 91
Marcus S. Zarra Avatar answered Jan 20 '23 14:01

Marcus S. Zarra