LK Phuc Nguyen LK Phuc Nguyen - 6 months ago 13
Swift Question

Swift - suggestion of CoreData implement for 2 data models

I am working on an app that I need to manage persistency for 2 data models.

The app is as simple as it will let me add courses I am taking first, then for each course, I will add some tasks to do.

Courses will display in a TableView, tapping on a course will show tasks in another screen which is also a table view.

I have made CoreData work for CourseModel, I think I will need TaskModel to keep Task table persistent but I am stuck at how to make them work together.

This is CourseModel: works with Course Table

Course
courseCode: String
courseTitle: String
courseDesc: String
list: CourseList (To One, inverse: members)

CourseList
members: Course (To Many)

CourseData
courseCode: String
courseTitle: String


This is TaskModel: works with Task Table

Task
taskTitle: String
taskDueDate: String
taskTag: String
list: TaskList (To One, inverse: members)

TaskList
members: Task (To Many)

TaskData
taskTitle: String
taskDueDate: String


I am using Swift and CoreData (not SQLite).

Anyone please suggest me a solution. Thank you!

Answer

I think it should be as simple enough as this:

Course
courseCode: String
courseTitle: String
courseDesc: String
tasks: Task (To Many, inverse: course)

Task
course: Course (To One, inverse: tasks)

A Course has many tasks and a Task belongs to a Course.