Core data 核心数据-关系

Core data 核心数据-关系,core-data,save,Core Data,Save,请让我知道如何通过关系保存对象。 如果我有两个实体,比如注释,类别实体 注:类别为一对一。 注释的多对一分类 如果注释中有类别上下文,如何保存 请给我一些意见 如何通过集合保存。那太好了 我有员工和部门实体。部门与员工之间存在一对多关系。员工与部门有一对一的关系。我想使用部门实体保存员工实体的对象 每次我为员工创建新对象时—— import UIKit import CoreData class ViewController: UIViewController { var conta

请让我知道如何通过关系保存对象。 如果我有两个实体,比如注释,类别实体 注:类别为一对一。 注释的多对一分类

如果注释中有类别上下文,如何保存

请给我一些意见

如何通过集合保存。那太好了



我有员工和部门实体。部门与员工之间存在一对多关系。员工与部门有一对一的关系。我想使用部门实体保存员工实体的对象

每次我为员工创建新对象时——

import UIKit
import CoreData
class ViewController: UIViewController {

    var container: NSPersistentContainer? = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    var empSet = NSSet()
    var empS = Set<EmployeeExample>()
    override func viewDidLoad() {
        super.viewDidLoad()
        var context:NSManagedObjectContext = (container?.viewContext)!
        let dept = NSEntityDescription.insertNewObject(forEntityName: "Department", into: context) as! Department
        let emp = NSEntityDescription.insertNewObject(forEntityName: "Employee", into: (container?.viewContext)!) as! Employee
        emp.firstName = "YYYY"
        emp.lastName = "HHHHHHH"
        empS.insert(emp)
        print("Count of Emp SSSS Set == \(empS.count)")
        let emp1 = NSEntityDescription.insertNewObject(forEntityName: "Employee", into: (container?.viewContext)!) as! Employee
        emp1.firstName = "RRRRR"
        emp1.lastName = "YYYYY"
        empS.insert(emp1)
        empSet.addingObjects(from: empS)
        dept.deptName = "CCC"
        print("Count of Emp SSSS Set == \(empS.count)")
        print("Count of Emp Set == \(empSet.count)")
        dept.addToEmp(empSet)
        do {
            try appDelegate.saveContext()
            print("Saved -------------")
        }catch {
            print("Error")
        }
    }



}
导入UIKit
导入CoreData
类ViewController:UIViewController{
var容器:NSPersistentContainer?=(UIApplication.shared.delegate作为?AppDelegate)?.persistentContainer
让appDelegate=UIApplication.shared.delegate为!appDelegate
var empSet=NSSet()
var empS=Set()
重写func viewDidLoad(){
super.viewDidLoad()
变量上下文:NSManagedObjectContext=(容器?.viewContext)!
让dept=NSEntityDescription.insertNewObject(forEntityName:“Department”,into:context)作为!Department
让emp=NSEntityDescription.insertNewObject(forEntityName:“Employee”,插入:(container?.viewContext)!)作为!Employee
emp.firstName=“YYYY”
emp.lastName=“hhhhh”
emp.插入(emp)
打印(“Emp SSSS集合的计数=\(Emp.Count)”)
让emp1=NSEntityDescription.insertNewObject(forEntityName:“Employee”,插入:(container?.viewContext)!)作为!Employee
emp1.firstName=“RRRRR”
emp1.lastName=“YYYYY”
环境管理计划插入(环境管理计划1)
empSet.addingObjects(from:empS)
部门名称=“CCC”
打印(“Emp SSSS集合的计数=\(Emp.Count)”)
打印(“Emp集合的计数=\(empSet.Count)”)
部门地址(empSet)
做{
请尝试appDelegate.saveContext()
打印(“已保存----------------”)
}抓住{
打印(“错误”)
}
}
}
在这些行中,我对如何获得newDepartment值表示怀疑。是否必须将let newDepartment=NSEntityDescription.insertNewObject(forEntityName:“Department”,插入:(container?.viewContext)!)声明为!系

这里的
部门
对象是应该与特定
员工
对象相关的任何对象。如何获得它取决于应用程序的工作方式,但您可能希望执行以下操作之一:

  • 如果该员工属于应用程序中不存在的新部门,则创建
    部门
    的新实例,并将该对象指定为
    员工.部门
  • 如果该员工所属的部门已经存在于您的应用程序中,则您可以使用已经存在的
    部门
    。您可能通过从核心数据中获取
    Department
    对象来实现这一点——使用
    NSFetchRequest
    或使用
    NSFetchedResultsController

  • 苹果公司提供了关于这一主题的详细信息,例如其核心数据编程指南的章节。有什么你不明白或有困难的地方吗?谢谢。在这些行中,我对如何获得newDepartment值表示怀疑。是否必须将let newDepartment=NSEntityDescription.insertNewObject(forEntityName:“Department”,插入:(container?.viewContext)!)声明为!部门我被困在这些队伍中。请帮助我建立员工和部门实体。部门与员工之间存在一对多关系。员工与部门有一对一的关系。我想使用department entity.import CoreData类ViewController:UIViewController{var容器:NSPersistentContainer?=(UIApplication.shared.delegate为?AppDelegate)?.persistentContainer让AppDelegate=UIApplication.shared.delegate为!AppDelegatevar empSet=NSSet()var empS=Set()保存员工实体的对象