提取到2个不同上下文中的NSManagedObject具有不同的属性值



我的应用程序有一个可重复的CoreData错误
我使用viewContext进行显示,使用backgroundContext进行对象更新。两个上下文都属于同一个CCD_ 3
在某个时刻,在将对象的status属性从2更新为1,并将其updatedAt属性从nil更新为Date()之后,我在backgroundContext中保存了一个对象。稍后,我想取回这个更新的对象,我的理解是fetch总是返回持久存储的内容
因此,无论提取到哪个上下文中,提取的对象都应该是相同的。然而,事实并非如此
我还将-com.apple.CoreData.ConcurrencyDebug 1设置为启动参数,因此这不是CoreData多线程错误。这是我的测试代码:

对象保存在此处:

let context = backgroundContext!
context.performAndWait {
assert(ItemStatus(rawValue: item.status) == .isBought)
item.status = ItemStatus.isToBuy.rawValue
item.updatedAt = Date()
_ = saveContext(context)
}  

带有

func saveContext(_ context: NSManagedObjectContext) -> Error? {
if !context.hasChanges { return nil }
let inserts = context.insertedObjects; if !inserts.isEmpty { print("Will save inserted objects: (inserts)") }
let updates = context.updatedObjects;  if !updates.isEmpty { print("Will save updated objects: (updates)") }
let deletes = context.deletedObjects;  if !deletes.isEmpty { print("Will save deleted objects: (deletes)") }
do {
try context.save()
print("(context.name!) saved")
} catch {
fatalError("Unresolved error")
}
return nil
}  

稍后,我使用将对象获取到两个上下文中

let mwFetchRequest = NSFetchRequest<Item>(entityName: Item.entityName)
let passwordPredicate = NSPredicate(format: "(Schema.Item.password) == %@", password)
let namePredicate = NSPredicate(format: "(Schema.Item.name) == %@", "Mineral water")
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [passwordPredicate, namePredicate])
mwFetchRequest.predicate = compoundPredicate
mwFetchRequest.returnsObjectsAsFaults = false
backgroundContext.performAndWait {
let bcItem = try! backgroundContext.fetch(mwFetchRequest)
print("backgroundContext: (bcItem)")
}
viewContext.performAndWait {
let vcItem = try! viewContext.fetch(mwFetchRequest)
print(„viewContext: (vcItem)")
}  

这是我在以下代码后设置断点时的日志:

Will save updated objects: [<ShopEasy.Item: 0x600000d7cf50> (entity: Item; id: 0x9698d776a7665623 <x-coredata://35BF43D6-4CF7-490D-B944-9DDFF2823AA1/Item/p1617>; data: {
buyPlaces = "<relationship fault: 0x600002e296a0 'buyPlaces'>";
fixedAtTopAt = nil;
howOftenBought = 1;
lastBoughtDate = "2021-01-24 13:02:09 +0000";
name = "Mineral water";
password = "PW_1";
status = 1;
updatedAt = "2021-01-24 13:32:14 +0000";
})]
backgroundContext saved
…
backgroundContext: [<ShopEasy.Item: 0x600000d7cf50> (entity: Item; id: 0x9698d776a7665623 <x-coredata://35BF43D6-4CF7-490D-B944-9DDFF2823AA1/Item/p1617>; data: {
buyPlaces = "<relationship fault: 0x600002e296a0 'buyPlaces'>";
fixedAtTopAt = nil;
howOftenBought = 1;
lastBoughtDate = "2021-01-24 13:02:09 +0000";
name = "Mineral water";
password = "PW_1";
status = 1;
updatedAt = "2021-01-24 13:32:14 +0000";
})]
viewContext: [<ShopEasy.Item: 0x600000d75ae0> (entity: Item; id: 0x9698d776a7665623 <x-coredata://35BF43D6-4CF7-490D-B944-9DDFF2823AA1/Item/p1617>; data: {
buyPlaces =     (
"0x9698d776b10e5621 <x-coredata://35BF43D6-4CF7-490D-B944-9DDFF2823AA1/Place/p971>"
);
fixedAtTopAt = nil;
howOftenBought = 1;
lastBoughtDate = "2021-01-24 13:02:09 +0000";
name = "Mineral water";
password = "PW_1";
status = 2;
updatedAt = nil;
})]  

显然,对象首先是使用backgroundContext正确保存的,因此应该在持久存储中
然后将其正确地取回backgroundContext
但是在将同一对象获取到viewContext之后,两个更改的属性status和updatedAt的值与保存之前的值相同。

我的问题:
我的假设错了吗?我的代码有问题吗?

稍后,我想取回这个更新的对象fetch总是返回持久存储的内容。

fetch根据持久存储的内容选择要返回的对象,但默认情况下不会根据存储的内容更新对象的内存副本。有一种选择可以这样做,根据我的经验,这是行不通的。要更新存储中的现有对象,可以刷新它或在上下文中设置合并,以便自动传播对存储的更改。

最新更新