Hi there, I'm Marcos!

Creating CoreData objects with relationships

If you need to create some CoreData instances that have a relationship between them you can do the following.

First you create the entities:

// Create the first item
let project = Project(context: context)
project.id = UUID()
project.name = "Test project"

let task = Task(context: context)
task.id = UUID()
task.name = "Task name"

Then you need to assign one entity to the other through the property that contains the relationship:

task.project = project

You can use this to create new entities throughout your code, it is particularly useful when creating previews for your views if they use both the parent and child entities.