nalexn / clean-architecture-swiftui

SwiftUI sample app using Clean Architecture. Examples of working with CoreData persistence, networking, dependency injection, unit testing, and more.
MIT License
5.73k stars 697 forks source link

How to save related CoreData objects? #69

Closed riley-usagi closed 2 years ago

riley-usagi commented 2 years ago

Hi, @nalexn .

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

Thanks.

nalexn commented 2 years ago

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)