Core Data saving problem: can't update transformable attribute (NSArray)
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开发者_JAVA技巧:&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...
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.
精彩评论