簡體   English   中英

如何在Swift 3中使用核心數據為多個部門保存一條記錄

[英]How to save one record for multiple department using core data in swift 3

我有兩個表名為EmployeeDepartment ,我已經按照以下方式給出了關系,

見下面我的部門表

在此處輸入圖片說明

這是我的員工表

在此處輸入圖片說明

   func saveEmpDetails(empId: String, andEmployeeName empName: String, andDeptId deptId: String, andDeptName departName: String, andProfile profileName: String, createdOn minutesAgo:Double , empQuote quote:String) {

            // Reading AppDelegate
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            // Getting managed object context
            let managedObjectContext = appDelegate.persistentContainer.viewContext
            let empID:Int? = Int(empId)

            let _predicate = NSPredicate(format: "empId=%@", empId)

            let _fetchRecord: Employee? = isRecordExist(withEntiryName: "Employee", with: _predicate) as? Employee


            if _fetchRecord != nil {

                // Update Employee record as managed objext


            }else {

                // Inserting Employee record as managed objext
                let _empInformation: Employee? = NSEntityDescription.insertNewObject(forEntityName: "Employee", into: managedObjectContext) as? Employee

                _empInformation?.empId = Int32(empID!)
                _empInformation?.empName = empName
                _empInformation?.empProfile = profileName
                _empInformation?.createdOn = Date().addingTimeInterval(-minutesAgo * 60) as NSDate
                _empInformation?.empQuote = quote

                let departmentRecord: Department? = NSEntityDescription.insertNewObject(forEntityName: "Department", into: managedObjectContext) as? Department
                //            departmentRecord?.departName = departName
                //            departmentRecord?.departId = Int32(deptId)!
                //            departmentRecord?.addToEmployees(_empInformation!)

                let allDepartments = NSSet(array : ["10", "11"])  // Set<String>
                departmentRecord?.addToEmployees(allDepartments)
                departmentRecord?.addToEmployees(_empInformation!)
            }

            // Saving the employee details using save context
            appDelegate.saveDBContext()
        }

現在我想為多個部門保存一份記錄,假設ABC員工在iOS部門和Android中工作

我的問題是:我如何一次保存該ABC員工記錄。 我付出了很多努力並做了堆棧溢出,但沒有遇到這種情況。

任何人都可以幫助完成我的教程。

將您的員工關系從“一對多”更改為“多對多”,以便兩個實體之間的關系變為“多對多”。 這樣,您的員工將擁有一組部門。 只需查找許多對許多核心數據關系,就可以找到很多有關它的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM