Search code examples
swiftcore-data

How to build complex relationships in CoreData correctly?


I am dealing with CoreData, for training, I decided to create a small application for recording user income and expenses. CoreData tutorials all contain To-Do-List examples, and I haven't found any good examples that would help me.

// MARK: - Grammar 
// I want to apologize for grammatical errors in the text. Unfortunately,
// English is not my native language, so in some places I used a translator.

If something is not clear, I will definitely try to explain it again.

When I began to think over how I would implement the application, I assumed that the most convenient way would be to save all user operations and make calculations in the application in the right places. So far, abstract, since It seems to me that this has little to do with the question, if you need to be more precise, I can provide a complete idea.

So, I'm going to save the user model, which will have the following data:

User operations (Operation type) - all operations will be saved, each operation includes the category for which the operation was performed, as well as the amount in currency.

User-selected categories (Category Type) - Categories that will be used for expenses or income when adding an operation.

Wallets (Type Wallet) - User's wallets, Everything is simple, the name, and the balance on it.

Budget Units (BudgetUnit Type) - These are user budgets, contains a category, and a budget for it. For example: Products - 10.000 $

Models picture

When I started building dependencies in CoreData, I got a little strange behavior.

strange behavior

That is, the user has a relationship on the same category model as the Budget Unit and Operation. Something tells me that it won't work that way. I want the user categories to be independent, he selected them, and I'm going to display them on the main screen, and each operation will have its own category model

I want

In the picture above, the category model is used 3 times, the same model.

This is roughly how I represent the data structure that I would like to see. Different models have their own category model, independently of the others.

I think it could be implemented using 3 different models with the same values, but it seems to me that this approach is considered wrong.

So how do you properly implement the data model so that everything works as expected? I would be grateful for any help!

--- EDIT ---

As a solution to the problem, I can create multiple entities as Category (Example bellow)

example

But I don't know if this is good practice


Solution

  • I looked into several other open source projects and saw a solution to the problem.

    I hope this helps someone in the future.

    There is no need to save the categories for the user, you can simply save the categories in the application by adding the IsSelected and ID parameter to them in order to change these parameters when you select a category, and immediately understand which ones you need to display.

    For budgets and operations (transactions) , we only need to save the category ID to immediately display the correct one.

    For example:

    Example

    Thanks @JoakimDanielson and @Moose for helping. It gave me a different view of the subject.