获取添加到 NSFetchedResultsController 的最新对象?
甚至不确定这是否可行,但这是用例:
我有一个 RootView tableview,它使用 NSFetchedResultsController 来管理列表。我点击一个添加按钮,其中显示了 AdditionViewController
。 AdditionViewController
使用单独的managementObjectContext
来创建新对象。保存时,对象被传回 RootView,其中新对象(在新的 managedObjectContext
中)被合并到主 managementObjectContext
中。然后 AdditionViewController 被关闭,显示 RootView。
我想要做的是将合并后加载的新对象推送到我的 DetailViewController
中,这样当 AdditionViewController
被解除时,显示完整的细节视图。
如何获取刚刚添加到 fetchedResultsController
的对象,以便将其传递给 DetailViewController
?
--更新--
仍然没有任何进展。让我尝试(希望)更好地解释一下我需要做什么。如果这仍然令人困惑,请提出问题。我很难思考如何描述这个问题。
我完全知道如何在保存新对象时将详细信息视图推到模式添加视图下方。问题是我要保存的对象位于其自己的 fetchedReaultsController 中。我正在将此 frc 合并到主 fetchedResultsController
中,因此,如果我尝试将对象发送到详细视图,则会崩溃,因为在模态添加视图被关闭,详细视图调用 viewWillAppear 。这就是我想要解决的问题。我怎样才能知道刚刚添加到主fetchedResultsController
中的对象以便将其发送到detailViewController?
--更新--
为任何可以告诉我如何从获取的结果控制器检索最近添加的对象的人添加赏金。或者如何在不知道其索引路径的情况下从获取的结果控制器检索特定对象。
Not even sure if this is feasible, but here's the use case:
I have a RootView tableview that uses NSFetchedResultsController
to manage the list. I tap an add button, which presents the AdditionViewController
. The AdditionViewController
uses a separatemanagedObjectContext
to create the new object. On Save, the object is passed back to the RootView, where the new object (in the new managedObjectContext
) is merged into the main managedObjectContext
. The AdditionViewController
is then dismissed, revealing the RootView.
What I would like to do, is to push my DetailViewController
with the new object loaded after the merge, so that when the AdditionViewController
is dismissed, the full detail view is revealed.
How can I get the object that has just been added to the fetchedResultsController
in order to pass it to the DetailViewController
?
--UPDATE--
Still nothing on this. Let me try to explain what I need to do (hopefully) a bit better. If this is still confusing, ask questions. I'm having a hard time thinking of how to describe the problem.
I am fully aware how to push the Detail view underneath the modal addition view upon saving the new object. The problem is that the object I am saving is in its own fetchedReaultsController
. I am merging this frc into the main fetchedResultsController
, so if I try to sent the object to the detailview, I get a crash, because the object has been invalidated (due to the merge) by the time the modal addition view is dismissed, and the detailview calls viewWillAppear
. That is what I am trying to get around. How can I figure out what object was just added to the main fetchedResultsController
in order to send it to the detailViewController?
--UPDATE--
Adding a bounty for anyone who can tell me how to retrieve the most recently added object from a fetched results controller. Or how to retrieve a specific object from a fetched results controller without knowing it's indexPath.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是我在几乎相同的用例中的做法:
AdditionViewController
时,用户可以选择保存他们创建的项目或从新项目对话框中取消。我将用户的选择传达回 RootViewController。DetailViewController
。Here's how I did it in an almost identical use case:
AdditionViewController
is displayed, the user has the option of saving the item they created or cancelling out of the new item dialog. I communicated the user's choice back to theRootViewController
.DetailViewController
.根据记录,答案是在保存上下文后获取addingManagedObjectContext 中对象的对象ID(因为ID 在保存后发生变化),并在合并后将该ID 传递给主 ManagedObjectContext。所需的完整代码如下(如果有人有更简单的方法,请告诉我)
感谢 frenetisch applaudierend为我指明了正确的方向。
For the record, the answer was to grab the object ID of the object in the addingManagedObjectContext AFTER saving the context (since the ID changes after saving), and passing that ID to the main managedObjectContext after the merge. The full code required for this is below (if anyone has an easier way, let me know)
Thanks to frenetisch applaudierend for pointing me in the right direction.