核心数据:设计问题。对象包装与否?

发布于 2024-08-06 06:49:17 字数 776 浏览 2 评论 0原文

我正在使用 Core Data(适用于 iPhone)设计我的第一个项目,我遇到了一些可能与我的设计方法相关的问题。

我正在开发一个应用程序,允许用户创建订单(假设是一家餐厅)。 我正在使用图形设计器来建模我的持久性对象(即OrdeMO)。我将 MO 添加到每个名称的开头以指示其是托管对象。 我使用 XCode 自动创建托管对象类。

我创建了一些“DAO”类,允许您在托管上下文中搜索或创建新对象。

现在我的问题。

我想创建一个 OrderMO 对象来存储用户正在创建的订单,但我不希望它成为上下文的一部分,直到用户实际放置它。 我尝试使用 [OrderMO alloc] 创建对象,但是我得到的对象“不完整”,当我尝试设置其任何属性时,我收到错误。

我假设问题是我需要在上下文中创建订单才能使用它。是这样吗?

我考虑了各种选项:

  1. 在上下文中创建对象,并在用户放弃订单时进行用户回滚。问题是用户可能会在此过程中保存其他上下文对象(例如他的首选项),因此这不起作用。有没有办法在“单独的事务内”创建对象?

  2. 创建一个包装对象,该对象将保存与 MO 相同的数据,然后仅在用户下订单时创建 MO。这样做的缺点是我必须维护一个新类。

  3. 在 MO 中创建一个属性,例如“已放置”,并用于在上下文中过滤我的搜索。这个问题的问题是,我最终会在域中得到“垃圾”对象(即未下达的订单),并且我必须不时进行一些清理...

我还有其他选择吗?

任何建议表示赞赏。

谢谢(阅读这篇长文!) 贡索

I'm designing my first project using Core Data (for iPhone) and Im having some issues that might be related with my design approach.

I'm doing an application that allows the user to create an Order (let's say for a restaurant).
I'm using the graphic designer to model my persistence object (i.e. OrdeMO). I add MO to the ead of each name to indicate its a Managed Object.
I use XCode to create the Managed Object Class automatically.

I have created some "DAO" classes that allows you to search or create a new object in the Managed Context.

Now to my problem.

I want to create an OrderMO object to store the order the user is creating, BUT I don't want it to be part of the context until the user actually places it.
I tried creating the object with [OrderMO alloc] but the object I get is "incomplete" and when I try to set any of its attribute I get an error.

I'm assuming the problem is that I need to create the order IN the context in order to use it. Is that so?

I have considered various options:

  1. Create the object in the context and the user rollback if the user discards the order. The problem is that the user might save other context object during the process (like his prefs) so this doesn't work. Is there a way to create the object "inside a separate transaction" of sorts?

  2. Create a wrapper object that will hold the same data as the MO, and then only create the MO when the user place the order. The downside of this is that I have to maintain a new class.

  3. Create an attribute in the MO, such as "placed", and use to filter my searches in the context. The problem with this one is that I will end up with "trash" objects in the domain (i.e. unplaced orders) and I will have to do some cleanup from time to time...

Do I have any other choice?

Any suggestion is appreciated.

Thanks (for reading this long post!)
Gonso

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2024-08-13 06:49:17

您应该在托管对象上下文中创建 OrderMO 对象,然后 如果用户决定不下订单,请删除
如果在删除对象之前保存了上下文,则“垃圾”对象将在下次保存时从持久存储中删除(如果未保存上下文,则“垃圾”对象将永远不会保存到持久存储中) .
确定是否已下订单的标志不必像您在选项 3 中建议的那样位于 OrderMO 对象中。它可以位于正在跟踪正在编辑的订单的视图控制器中。而且,同样,您不会有“垃圾”对象,因为它们将被删除。

You should create the OrderMO object in the managed object context and then delete it if the user decides not to place the order.
If the context is saved before the object is deleted, the "trash" object will be deleted from the persistent store on the next save (if the context wasn't saved, the "trash" object will never be saved to the persistent store).
The flag to determine if the order was placed or not does not have to live in the OrderMO object as you suggest in option 3. It could be in the view controller that is tracking the order(s) that are being edited. And, again, you won't have "trash" objects because they will have been deleted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文