核心数据销售登记策略
我一直在尝试在核心数据中使用第二个托管对象上下文(MOC)来添加销售,该销售将在 NSTableView 中列为待处理,直到用户按下按钮来处理交易。
按下按钮后,第二个上下文将被保存,并且 NSManagedObjectContextDidSaveNotification 会在主上下文上触发 mergeChangesFromContextDidSaveNotification 。
到目前为止还可以。但是,接下来我需要清除第二个上下文,以便以下销售交易位于空白 MOC 上。还可以通过取消操作或更改用户来清除它。
看来清除第二个 MOC 是不可能的……在寻找解决方案后,我确信这不是一个可行的核心数据模式。相反,我现在回溯到仅使用一种上下文。但是,当前销售现在显示所有交易,而我只想要当前的交易。再说一遍,如果我可以在那里列出当前“待处理”的交易,那么可以使用“取消”按钮来撤消这些交易(如有必要);并清除“处理销售”按钮后的表格,将实现类似的管理交易的方式。
所以我现在陷入困境,因为没有明显的方法可以从其他最近的交易中识别“当前”/待处理的交易。
我需要两步数据输入。 (1) 添加销售、编辑等。 (2) 流程,即提交这些销售
因此,第一个策略现在看起来原则上可行,并由 ( http://developer.apple.com/library/mac/#documentation/DataManagement/Devpedia-CoreData/managementObjectContext.html )以及本教程 ( http://www.timisted.net/blog/archive/multiple-management-object-contexts-with-core-data/ )
但是该教程中使用的方法只保留一个第二个上下文中的托管对象并根据需要复制/删除。
最好的策略是使用第一种方法和第二个 MOC,然后复制和复制。删除交易而不是合并?
还是有一个我还没有看到的更简单的解决方案?
希望这个问题有意义:-) 并感谢任何帮助
I have been trying to use a second managed object context (MOC) in core data to add sales which will be listed in an NSTableView as pending, until the user presses a button to process the transactions.
Upon pressing the button, the 2nd context is saved, and the NSManagedObjectContextDidSaveNotification does trigger mergeChangesFromContextDidSaveNotification on the main context.
So far OK. But, next I need to clear the second context so that the following sale transaction is on a blank MOC. Also to clear it by a cancel action or change of user.
Seems that the clearing of the second MOC is not possible... and I have become convinced after searching for a solution that this is not an viable core data pattern. Instead I am now backtracking to use only one context. However, the current sale now displays all transactions, where I would only want the current ones. Again, if I could list the current "pending" transactions there, have a "cancel" button to undo these if necessary; and clear the table after the "process sale" button, would achieve similar means of managing the transactions.
So I am now stuck since there is no obvious way to identify "current" / pending transactions from other recent ones.
I need a two step data entry. (1) add sales, edit, etc. (2) process i.e. commit these sales
So the first strategy now seems in principle OK and is suggested by ( http://developer.apple.com/library/mac/#documentation/DataManagement/Devpedia-CoreData/managedObjectContext.html ) and also in this tutorial ( http://www.timisted.net/blog/archive/multiple-managed-object-contexts-with-core-data/ )
Yet the method used in that tutorial only keeps a single managed object in the 2nd context and copies / deletes as required.
Is the best strategy to use the first approach with a second MOC, and copy & delete the transactions rather than merge ?
Or is there a simpler solution that I am not seeing yet ?
Hope this question makes sense :-) and any help appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需为您的交易实体引入一个状态属性,您可以在其中保存有关待处理交易的信息。录入时,要经常保存(由于数据很少,所以成本也不算太高)。通过这种方式,您可以完全关闭应用程序,并通过从持久性存储中重建相同的情况来准确地恢复用户离开的位置...
无论如何,您迁移到一个 MOC 的策略都是正确的。
Simply introduce a status attribute for your transaction entity where you keep the information about pending transactions. During entry, save frequently (which is not too expensive because of very little data). In this way, you can shut down the app completely and resume exactly where the user left off by reconstructing the same situation from the persistent store...
In any case, your strategy to move to one MOC is the right one.
关于实现解决方案的技术细节,我想分享该方法,因为似乎很难理解接口构建器与将谓词应用于结果相关的意义。
实际上,我浪费了一些时间寻找一种利用绑定和界面生成器的便捷方法。
因此,似乎有效的方法是为每个输出表添加自定义 NSArrayController。在待销售表中,绑定到SalesController;我只是将 NSArrayController 子类化,然后在
awakeFromNib
中进行更改,如下所示。注释掉的行是完全没有影响的尝试,所以我不太理解 fetch 谓词的作用,但设置过滤谓词确实具有预期的效果。对于列出已确认的交易,其相同,但带有“pending==NO”On the technicality of implementing the solution, wanted to share the method, as it seems very hard to make sense of interface builder in relation to applying a predicate to the results.
Actually I have wasted some time looking for a convenient way to leverage the bindings and interface builder.
So, what seems to work is to add custom NSArrayControllers for each output table. In the pending sales table, bind to the SalesController; I just subclass NSArrayController and then make the change in
awakeFromNib
as shown below. The commented out line is the attempt that made no impact at all, so I don't really understand the role of fetch predicate but setting a filter predicate does have the desired effect. For listing confirmed transactions, its the same but with "pending==NO"