Do I need to do a data migration every time I update source data of my core data entities?

49 views Asked by At

I am fairly new to swift and core data. I am building an app where uses input ingredients and get recipes. For now, recipe data is loaded in from JSON and turned into instances of core data recipe entities when the user first loads in the app. The current implementation of this is to call the below function on app load and load in the recipe data based on whether the variable "recipesLoaded" is not true. However, this does not seem to be called when the app is updated, only when it is ever first opened.

    func loadRecipesIfNeeded() {
        let defaults = UserDefaults.standard
        
        if !defaults.bool(forKey: "recipesLoaded") {
              // create instances of core data recipe entity 
              defaults.set(true, forKey: "recipesLoaded")
        }

However, I want the ability to add recipes to the source data without reloading every single old recipe as well. By only loading in the new entities, I hope to maintain the attributes of the old recipes (e.g. if the favourite attribute has been set to true).

Can anyone suggest a good way to go about this? I've been thinking I could create a User core data entity and track what version update they are up to (and match the app versioning to the recipe data versioning) and then only load in the missing versions. But I'm unsure if there is a better way.

Thank you.

0

There are 0 answers