gpt4 book ai didi

iphone - 是需要一个单独的 NSManagedObjectContext 来回滚用户的 coredata 更改不好吗?

转载 作者:行者123 更新时间:2023-11-29 04:53:53 25 4
gpt4 key购买 nike

是否需要单独的 NSManagedObjectContext 来回滚用户不同意的 coredata 更改?

我在 CoreDataBooks example 中注明它使用一个单独的 NSManagedObjectContext。我想问的是这是否是处理该要求的唯一真正方法,如果不是,替代方法是什么。

要求是:

  • 允许用户开始更改页面上对象中的字段
  • 这可能包括传递到单独的模态视图以帮助他们设置此数据
  • 提出“确定”或“取消”以决定是否继续进行更改
  • 作为进行更改的一部分,您仍然继续使用 CoreData 托管对象(即不创建一组单独的对象/方法来处理此问题)
  • 所以,假设您想坚持使用现有的核心数据对象,那么要支持 OK 和 CANCEL 更改,是通过拥有单独的第二个 NSManagedObjectContext 来实现此目的的唯一方法吗?

摘录示例供引用

Creates a new book, an AddViewController to manage addition of the book, and a new managed object context for the add controller to keep changes made to the book discrete from the application's managed object context until the book is saved. IMPORTANT: It's not necessary to use a second context for this. You could just use the existing context, which would simplify some of the code -- you wouldn't need to merge changes after a save, for example. This implementation, though, illustrates a pattern that may sometimes be useful (where you want to maintain a separate set of edits).

最佳答案

创建一个单独的NSManagedObjectContext是迄今为止处理您所描述的情况的最简单方法。如果您想在单独的线程上进行更改,这也是唯一的方法。

请务必在保存期间使用 -[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:(NSNotification *)notification] 以使主 NSManagedObjectContext 正确同步。

关于iphone - 是需要一个单独的 NSManagedObjectContext 来回滚用户的 coredata 更改不好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8349725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com