I have a strange problem in my Core Data app.
I have three entities in my app, but today I found a problem with one of them. My problematic entity is called Invoice
and it has many attributes, including Products
. It's encoded NSArray of NSDictionaries (via default NSValueTransformer).
Everything works fine - i create my invoice, its client, its products, etc. Everything works.
But, when I choose my invoice from a list and then try to edit its products and click 'Save' button, my save works only until my app gets terminated.
The problem is only with my products
array - the rest (e.g. payment date, client etc.) saves.
What am I doing
I pass my Invoice
object via
NSManagedObject *inv = [self.fetchedResultsController objectAtIndexPath:indexPath];
invoiceEditor.invoice = inv;
And save my data (in my InvoiceEditor
VC):
[self.invoice setValue:client forKey:@"Client"] // NSDictionary;
[self.invoice setValue:products forKey:@"Products"] // NSArray of NSDictionaries;
[self.invoice setValue:pmDate forKey:@"PaymentDate"] // NSDate;
// other attributes
NSManagedObjectContext *context = self.invoice.managedObjectContext;
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
Everythings saves until being terminated: client, products, dates. But only products are 'reseted' after termination.
Anybody?
Just found the problem. From Model Object Implementation Guide:
And my
products
array was actually aNSMutableArray
instance. I solved it by copyingproducts
(so I converted mutable array to immutable array). Now it works. :) And I didn't need to start the bounty...Indeed, you've just found the problem. Sorry I just read your problem, but CoreData has several problems during saving.
Each attribute in a CoreData Entity must be immutable objects.