[英]Core Data saving problem: can't update transformable attribute (NSArray)
我的核心數據應用程序中有一個奇怪的問題。
我的應用程序中有三個實體,但今天我發現其中一個有問題。 我有問題的實體稱為Invoice
,它有許多屬性,包括Products
。 它是 NSDictionaries 的 NSArray 編碼的(通過默認的 NSValueTransformer)。
一切正常 - 我創建我的發票、它的客戶、它的產品等。一切正常。
但是,當我從列表中選擇我的發票,然后嘗試編輯其產品並單擊“保存”按鈕時,我的保存僅在我的應用程序終止之前有效。
問題僅在於我的products
陣列 - rest(例如付款日期、客戶等)保存。
我在做什么
我通過我的Invoice
object 通過
NSManagedObject *inv = [self.fetchedResultsController objectAtIndexPath:indexPath];
invoiceEditor.invoice = inv;
並保存我的數據(在我的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();
}
一切都保存到被終止:客戶、產品、日期。 但只有產品在終止后“重置”。
有人嗎?
才發現問題。 來自Model Object 實施指南:
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.
而我的products
數組實際上是一個NSMutableArray
實例。 我通過復制products
解決了它(所以我將可變數組轉換為不可變數組)。 現在它起作用了。 :) 而且我不需要開始賞金......
確實,您剛剛發現了問題所在。 抱歉,我剛剛閱讀了您的問題,但 CoreData 在保存過程中存在一些問題。
CoreData 實體中的每個屬性都必須是不可變對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.