nalexn / clean-architecture-swiftui

SwiftUI sample app using Clean Architecture. Examples of working with CoreData persistence, networking, dependency injection, unit testing, and more.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to save related CoreData objects?

riley-usagi opened this issue · comments

Hi, @nalexn .

Could you please explain how to save (store) related objects (Parent -> Childs) to the database?

Thanks.

Hey @riley-usagi , you simply insert both parent and child objects in the managed context (calling insertNewObject on a concrete NSEntityDescription) and then assign the references between the objects. Here is an example in this repo, where a Country has 1-many relationship with NameTranslation (check out xcdatamodeld file)