NSIncrementalStore 使用 NSSaveChangesRequest.deletedObjects == nil 调用

NSIncrementalStore called with NSSaveChangesRequest.deletedObjects == nil

我正在编写一个由 REST 服务支持的 NSIncrementalStore。它适用于 POST 和 GETing 对象,但是当删除它们时我遇到以下问题:

  1. 测试通话 [context delete: object];
  2. context.deletedObjects 包含 object(正如人们所期望的那样)
  3. 测试通话 [context save: &error];
  4. iOS 用 NSSaveChangesRequest 调用 NSIncrementalStores executeRequest:withContext:error:;当调用同时到达 context.deletedObjects == nilsaveChangesRequest.deletedObjects == nil 时,所以我的代码无法找出要从服务器中删除的对象。

在调用上下文的 save 和调用上下文的 NSIncrementalStore 之间,deletedObjects 怎么会被设置为 nil

更新 可能的原因:object.objectID 仍然是 temporaryID 之后的 saveNSIncrementalStore 当前正在从 newObjectIDForEntity:referenceObject: 获取对象 ID,Apple documentation 的描述似乎还不适用:

New objects inserted into a managed object context are assigned a temporary ID which is replaced with a permanent one once the object gets saved to a persistent store.

问题是我还没有在服务器上存储足够的信息来在获取期间重新创建(永久)对象 ID。

这解决了 NSIncrementalStore 中的问题:

  • executeRequest:withContext:error: 收到 NSSaveChangesRequest 时,它使用 referenceObjectForObjectID: 从保存的对象中提取(在我的例子中)引用字符串并存储这些(连同对象的其他内容)属性)在服务器上。

  • executeRequest:withContext:error: 收到 NSFetchRequest 时,它从服务器获取引用字符串(以及对象的其他属性)并使用 [context objectWithID: [self newObjectIDForEntity: entity referenceObject: referenceString]] 重新创建对象。