Core Data relationships. Do I need to set relationships between objects

Viewed 22

I have a question related to Core Data. As I've don't use it for some time, I need some refresher.

I have 2 entities Category and Task. Category can have many Tasks.

So I have 2 api request that returns categories and tasks

Category has id Task has id and categoryId

So If I have two such request what I need to do to have them inserted to Core Data and related correctly.

A. Does it suffice to just do 1. insert categories to core date 2. insert task to core data (can I do NSBatchInsertUpdateRequest?) Here I assume as Task object has categoryId field the relationship will be created automagically. So when I next request categories each will be pointing to their list of tasks and otherwise each task will point to its category

B. I need to set this relationship manually it

  1. Insert categories
  2. Insert task and for each task object set category for it like task.category = category so i will need to create this category in place like Category(context: context) or request this Category based on task.categoryId

C. Set this relationship on both side like task.category = category category.tasks = tasks

0 Answers
Related