首页 > 解决方案 > Swift Core Data - 更新连接到单个用户的对象

问题描述

在我的核心数据中,我有两个表“用户”和“课程”,它们以多对多关系连接。用户可以通过 .addCourseToList() 将课程添加/注册到他们的帐户,但是当我尝试使用 course.hasStarted = true 更新某个用户的课程列表中的课程对象时,更改将应用​​于课程本身和所有用户,而不是进行更改的个人。

向用户添加课程:

    func addToWishlist(course: Course) {
    signedInUser.addToWishlist(course)
    DataManager.shared.saveContext()
}

检索用户课程:

if let bought = signedInUser.bought?.allObjects as? [Course]{
        allCourses.append(bought)
    }

做出改变:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let course = allCourses[indexPath.section][indexPath.row]
    print(course.progression)
    if course.progression >= 1.0 {
        course.progression = 1.0
    } else {
        course.progression += 0.1
    }
    tableView.reloadData()
    DataManager.shared.saveContext()
}

数据库模式

标签: swiftcore-datarelationship

解决方案


推荐阅读