Core数据保存问题:无法更新可变属性(NSArray) [英] Core Data saving problem: can't update transformable attribute (NSArray)
问题描述
我在Core Data应用程式中有一个奇怪的问题。
我的应用程式中有三个实体,但今天我发现其中一个存在问题。我的问题实体叫做 Invoice
,它有很多属性,包括 Products
。它是NSDictionaries的NSArray编码(通过默认NSValueTransformer)。
一切正常 - 我创建我的发票,客户端,产品等。
但,当我从列表中选择发票,然后尝试修改其产品并点击保存按钮时,我的保存仅在我的应用获取
问题只在于我的产品
数组 - 其余的(例如付款日期,客户等)
b
我通过
传递发票
= [self.fetchedResultsController objectAtIndexPath:indexPath];
invoiceEditor.invoice = inv;
并保存我的数据(在我的 InvoiceEditor
VC):
[self.invoice setValue:client forKey:@Client] // NSDictionary;
[self.invoice setValue:products forKey:@Products] // NSDictionaries的NSArray;
[self.invoice setValue:pmDate forKey:@PaymentDate] // NSDate;
//其他属性
NSManagedObjectContext * context = self.invoice.managedObjectContext;
NSError * error = nil;
if(![context save:& error]){
NSLog(@Unresolved error%@,%@,error,[error userInfo]);
abort(); Everythings保存直到被终止:客户端,产品,日期。
}
<
p>刚刚发现问题。从模型对象实现指南:
如果有一个类的mutable和immutable版本,如NSArray和NSMutableArray - 你通常应该将get访问器的返回值声明为不可变对象,即使模型内部使用了一个可变对象。
而我的产品
数组实际上是一个 NSMutableArray
实例。我通过复制 products
解决了(所以我将mutable数组转换为immutable数组)。现在它的工作。 :)我不需要开始赏金...
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...
这篇关于Core数据保存问题:无法更新可变属性(NSArray)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!