Learn Courses My Dashboard

CoreData restoring data from web, is there a better way?


I created a function which accepts an array of my entity object which I encoded from the JSON file I stored and retrieved from the Web

My current implementation goes like this:

func restoreCoreDataFromArray(wordSource : [Words], collSource : [Collections]) {
        collSource.forEach { col in
            let cltn = Collections(context: container.viewContext)
            cltn.id = col.id
            cltn.name = col.name
            cltn.dateUpdated = Date()
        wordSource.forEach { w in
            let wds = Words(context: container.viewContext)
            wds.character = w.character
            wds.meaning = w.meaning
            wds.id = w.id

        try! container.viewContext.save()
        // TODO: add other entity in the future, when implemented

is there a better or efficient way of implementing this?

Note that and are already same object type as created in CoreData Model

Thanks in advance