I have an app I am working on, and it has a number of pieces of data that are interdependent. Basically, I have a set of number fields that are used to calculate other number fields, which are then used to calculate other fields. I am trying to determine the best way to handle the potentially large number of changes.
One solution I thought of is that I could override the setters of the values that can be modified by the user to do the calculations and then update the other values in data object appropriately. The largest downside to this is the sheer amount of (somewhat boilerplate) code that needs to be written.
Another solution I thought of is using Key-Value Observing to handle the data changes, and to then update the dependent values from those changes. Note that none of the dependent values will ever update except when an independent value updates. While I think this will be a bit more elegant, I am concerned about potential performance problems, or problems that I am possibly not considering.
Is there a third way that I am missing? Are there any best practices for this type of work? I know that I am likely going to have to write a bunch of boilerplate code of some sort – I would like the code to be the easiest to maintain, and the smallest amount of code possible.
KVO shouldn’t have any performance issues unless you have very many fields or very frequent updates, but its API isn’t the greatest. One downside is that it funnels all notifications through a single method. This may or may not be an issue depending on your number and types of fields.
Notifications might also be a good option. Each field could have a notification name that it would post changes from, and other fields could observe without an explicit pointer to the source field.
There is also delegation with protocols which, while simpler and more performant, can be rather boilerplate-heavy depending on how you do it.