KVO, Bindings and Undo inconsistent
KVO, Bindings and Undo inconsistent
- Subject: KVO, Bindings and Undo inconsistent
- From: Anthony Ballard <email@hidden>
- Date: Thu, 09 Jun 2011 06:01:36 +0200
I was hoping that someone could help me with undo, kvo and bindings.
I have an object type called Transaction. And I have an object type called Account. Transaction has a property called "value" and Account has a property called "balance". An account may have multiple transactions associated to it. In my testing environment, I only have 1 transaction and 2 accounts.
In the awakeFromInsert and awakeFromFetch methods of the transaction, i begin observing its own account. If the account is ever changed to a different account, then observeValueForKeyPath:ofObject:change:context: is successfully called on the transaction. The code then continues to remove its value from the balance of the old account and then adding itself to the balance of the new account.
This works perfectly and when the transactions account changes, the values reflect correctly for the balances of the two accounts.
The problem is when I hit cmd+z (undo). I expected it to undo the changes made. which it does do....... sometimes. This is the part that kills me. Let me explain.....
I have account 1 with a transaction of $500 associated to it. The balance of account 1 is $500 and the balance of account 2 is $0.
Now i move the transaction from account 1 to account 2. The observing works beautifully and the values are updated. Account 1 has $500 removed from it and Account 2 has $500 added to it.
So the balances after moving the transaction is: Account 1 $0 and Account 2 $500.
So you hit undo and expect everything to return back to the way it was, with the transaction associated back with account 1 and account 1's balance being $500 and account 2's balance being $0.
so here is the catch, it works, sometimes. I will run the app, and go through the above procedure. it works fine. I will then kill the app, run it again and go through the exact same steps, however the next time the results are different. some times when I undo, account 2 will end up with double the value of the transaction removed. i.e. the balance is not $0 but -$500. other times account 1 gets double the transaction value added. so after the undo, you would expect the balance of account 1to be $500, but it will show $1000.
The results are intermittent. Some times it works, some times it don't.
I thought maybe it would be because i change the transactions account, when I undo, the undo manager reverts the values of the accounts and then in addition, it calls observeValueForKeyPath:ofObject:change:context: because the account of the transaction has once again changed. So, when undoing, account 2 would have its value set back to $0, then the method called reduces it by a further $500, leaving it at -$500. the same would apply for account 1. The undo manager would revert the value of account 1 back to $500, like it was before the change, but then the method then adds an additional $500.
But then what I really don't understand is the intermittent values. Sometimes it works fine, other times only one value has double added, other times its both. I'm at a loss.
If it is the problem mentioned above, how do i prevent observeValueForKeyPath:ofObject:change:context: from being triggered/called when I undo?
Thanks in advance
Anthony_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden