Cocoa 绑定:更改时获取旧值
我正在编写一个核心数据 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设账户实体与交易具有反比关系。 (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
ofNSManagedObject
for the account entity, andTransaction
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
inAccount
so that theAccount
objects keep track of the balance themselves. I think it is more object-oriented-y to makeAccount
objects to take care of themselves than changing the balance from the side of theTransaction
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 implementingobserveValueForKeyPath:ofObject:change:context:
. The changes can be found in the dictionary passed to that method.