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:
If there are mutable and immutable versions of a class you use to represent a property—such as NSArray and NSMutableArray—you should typically declare the return value of the get accessor as an immutable object even if internally the model uses a mutable object.
And my products
array was actually a NSMutableArray
instance. I solved it by copying products
(so I converted mutable array to immutable array). Now it works. :) And I didn't need to start the bounty...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With