swift - 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()
}
解决方案
推荐阅读
- python - 如何处理混合大小写正则表达式?
- docker - 如何确保 docker 确认优雅退出?
- python-3.x - 如何编码以重新排列列中的所有文本
- python-3.x - 线程安全删除python中的目录
- javascript - 如何用Javascript来console.log增量变量如a1,a2,a3,a4?
- sharepoint - 如何使用上次提交的表单中的数据填充新表单上的 PowerApps 数据卡值
- android - MediaCodec 使用 inputSurface 编码视频但丢帧
- xpath - 如何获取所有子节点的文本
- python - 每行不满足条件的值的计数
- r - 在使用多个条件的情况下使用 dplyr 和 ifelse 创建新变量