Cocoa 绑定:更改时获取旧值

发布于 2024-08-19 00:24:15 字数 224 浏览 2 评论 0原文

我正在编写一个核心数据 Cocoa 应用程序,其中有帐户和交易(货币)。账户实体描述包含余额属性。交易实体描述与账户有关系。

当交易的帐户设置或更改时,我需要应用程序来更新帐户余额。例如,如果交易帐户从支票帐户更改为贷记帐户,则支票帐户和贷记帐户的余额都应更改以反映这一点。

我遇到的问题是我不确定如何确定交易的旧帐户,以便我可以更新其余额。我正在使用绑定。

有人能指出我正确的方向吗?

I am writing a core data Cocoa application in which there are accounts and transactions (monetary). The account entity description contains a balance attribute. The transaction entity description has a relationship to an account.

I need the application to update account balances when transactions have their accounts set or changed. For example, if a transaction's account is changed from checking to credit, the balances of both checking and credit should be changed to reflect this.

The problem I am having is that I am unsure how to determine the transaction's old account so I can update its balance. I am using bindings.

Can anyone point me in the right direction?

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

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

发布评论

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

评论(1

尛丟丟 2024-08-26 00:24:15

我假设账户实体与交易具有反比关系。 (Apple 强烈建议您始终保持反向关系。因此,如果您没有,请设置它!)

假设您有一个帐户实体的 NSManagedObject 子类 Account ,以及交易实体的 Transaction
将与交易的逆关系称为交易

然后,当您更改交易帐户时,CoreData 会自动更新反向关系。因此,您所要做的就是为 Account 中的交易编写一个自我观察例程,以便 Account 对象跟踪余额他们自己。我认为让 Account 对象照顾自己比从 Transaction 对象一侧改变余额更面向对象......当然,尽管如此这取决于你的口味。

要执行观察,您可以使用 KVO。基本上,您可以通过 addObserver:forKeyPath:options:context: 以及一组合适的选项来注册 KVO。然后,您可以通过实现observeValueForKeyPath:ofObject:change:context:来获取更改。可以在传递给该方法的字典中找到更改。

I assume that the account entity has the inverse relationship to the transactions. (Apple strongly suggests you always have inverse relationships. So if you haven't, please set it up!)

Let's say you have a subclass Account of NSManagedObject for the account entity, and Transaction for the transaction entity.
Call the inverse relationship to transactions as transactions.

Then, when you change the account for the transactions, the inverse relationship is automatically updated by CoreData. So, all you have to do is to write a self-observation routine for transactions in Account so that the Account objects keep track of the balance themselves. I think it is more object-oriented-y to make Account objects to take care of themselves than changing the balance from the side of the Transaction object... although of course it depends on your taste.

To perform the observation, you use KVO. Basically, you register the KVO by addObserver:forKeyPath:options:context: with a suitable set of options. Then, you get the change by implementing observeValueForKeyPath:ofObject:change:context:. The changes can be found in the dictionary passed to that method.

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