0
votes

I am trying to write a simple personal finance app for my own use and have the following issue and wondered if anybody can set me straight.

My data model is quite simple, I have 'account' managed objects which have an NSSet of 'transaction' managed objects which in turn have an NSSet of 'split' managed objects.

The 'split' object has a category and an amount so any transaction can be made up of multiple categories with differing amounts. e.g transaction total is £40 made up from £25 - Food and £15 - Fuel etc

The 'transaction' managed object can also have an optional 1 to 1 relation with another 'transaction' managed object. This is for when I want to represent a transfer. Therefore I have 2 'transaction' objects with the same attributes like date etc but each belongs to a different 'account' so shows up when I query for a list of transactions for an account.

I have overridden the appropriate setters on the 'transaction' managed object like setDate so that when this is called, it checks to see if it has another 'transaction' managed object linked to it and if it has, also changes the date of that transaction. That way changes made to one transaction are reflected in the other account transaction.

Still with me..? Now the problem is with the NSSet of 'split' objects for a transaction.

Lets say I created a new transaction in account A. I set the date for the transaction I create a 'split' object for the transaction which for arguments sake has a category of food for £20. In my UI, I then say this transaction is a transfer. This prompts me to pick the account I want to transfer to, I pick account B. In the background, a new 'transaction' object is created, its account is set to account B, I copy across the date etc from the original transaction in account a but I want to set both 'transaction' objects to point to the same NSSet of 'splits'.

That way, if I change the category or amount in the split of one of the transactions its reflected in the other? Changes to the simpler attributes are handled through the setters like setDate mentioned before. Can both 'transaction' objects point to the same NSSet of splits or do I manually have to synchronise the changes to both sets when a change is made?

Hope this makes sense and thanks for any help received.

2
Do transfers really have splits? What's the use case? - Wain

2 Answers

0
votes

Your relationship would need to be many:many between transaction and split entities.

If you want to do that then it would be better to create custom methods for setting both the transfer and splits rather than trying to override. You do need to write the logic for how one relationship is updated based on a change to another objects relationship. Once you have the relationship configured the objects at the other end are common so changes to the split instances are trivial.

It's also a good idea to be using mogenerator to manage your custom code separately to the auto generated code.

0
votes

I would suggest changing your model.

The Transaction entity is for data shared by the Split entity like date and description. Each Transaction always has at least 1 Split (or 2 Splits if you are making the system double entry accounting). You can expose the transaction properties through the Split entity.

Your Account entity won't have a direct relationship to Transaction. It will only have a direct relationship to the Split entity.