核心数据一对多关系在创建新关系时被删除

Core Data one-to-many relationship gets deleted when creating a new one

我是 Core Data 的新手,在探索它的工作方式时,我发现自己陷入了另一个问题。

我从两个单独的 Web 服务获取数据,一个获取 JSON 员工列表,另一个获取 JSON 公司列表。 公司将其员工列为 employeeID 数组,而员工将其工作的公司列为逗号分隔字符串,不作为参考。

所以我是这样做的:

1) 我确保从空数据库开始

2) 我从员工源中填充我的核心数据

3) 我填充我的公司实体,设置与他们拥有的每个员工的关系(这就是问题所在)

4) 我想显示公司及其关联员工的列表

问题是,一个公司可以雇佣几个员工,每个员工可以为0到n个企业工作。 因此,当我创建我的第一家公司时,我 link 它给它的员工并且一切正常,但是当我创建我的第二家公司并且 link 它给前一家公司的员工时,前一家公司失去了它与该员工的关系。

经过一些研究,我没有发现类似的问题(可能是因为我还是一个核心数据新手我没有找对地方),但我发现了这个post: http://codekea.com/3J1AZNYRkAMr/setting-core-data-relationship-reusing-same-object-removes-previous-relation.html 这似乎是完全相同的问题。 由于答案在我的案例中没有解决任何问题(代码将不再编译),我想我会创建一个示例项目来说明它。

该项目可以从这里下载测试: https://www.dropbox.com/s/qi65sh52wwe6ws3/test.zip?dl=0

我的代码如下(我用的是SwiftyJSON):

let mURLEmployees       = NSURL(string: "http://pastebin.com/raw.php?i=ZhmHCmwQ")
let mURLCompanies       = NSURL(string: "http://pastebin.com/raw.php?i=LCbvyvqv")

override func viewDidLoad() {
    super.viewDidLoad()

    //==============================================
    // 1)
    //      EMPTY THE DATABASE
    //==============================================


    var error: NSError? = nil

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context = appDelegate.managedObjectContext!

    let request = NSFetchRequest(entityName: "Employee")

    let entities = context.executeFetchRequest(request, error: nil) as! [NSManagedObject]

    for entity in entities {
        context.deleteObject(entity)
    }

    let request2 = NSFetchRequest(entityName: "Company")

    let entities2 = context.executeFetchRequest(request2, error: nil) as! [NSManagedObject]

    for entity in entities2 {
        context.deleteObject(entity)
    }

    if !context.save(&error) {
        println("An error occured while deleting entities : \(error?.localizedDescription)")
    }


    //==============================================
    // 2)
    //      POPULATE EMPLOYEES
    //==============================================



    let entityEmployee = NSEntityDescription.entityForName("Employee", inManagedObjectContext: context)


    let JSONEmployeesString = NSString(contentsOfURL: mURLEmployees!, encoding: NSUTF8StringEncoding, error: &error) as! String

    var dataJSON = JSONEmployeesString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

    var json = JSON(data: dataJSON!)

    if let employees = json["features"].array {
        for employee in employees {

            let employeeObject = Employee(entity: entityEmployee!, insertIntoManagedObjectContext: context)

            employeeObject.name = employee["properties"]["NAME"].string!
            employeeObject.companies = employee["properties"]["COMPANIES"].string!

            if !context.save(&error) {
                println("An error occured while saving employees : \(error?.localizedDescription)")
            }

        }
    }



    //==============================================
    // 3)
    //      POPULATE COMPANIES
    //==============================================



    let JSONCompaniesString = NSString(contentsOfURL: mURLCompanies!, encoding: NSUTF8StringEncoding, error: &error) as! String

    dataJSON = JSONCompaniesString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

    json = JSON(data: dataJSON!)
    if let companies = json["features"].array {
        for company in companies {


            let employees = company["properties"]["EMPLOYEES"].array!

            var employeeObjects = NSMutableSet()

            for employee in employees {

                let predicateEmployeeName = NSPredicate(format: "name == %@", employee.string!)
                let DBRequestEmployeeObject = NSFetchRequest(entityName: "Employee")
                DBRequestEmployeeObject.predicate = predicateEmployeeName
                let EmployeeResultArray = context.executeFetchRequest(DBRequestEmployeeObject, error: nil) as! [Employee]
                if let EmployeeResult = EmployeeResultArray.first {
                    employeeObjects.addObject(EmployeeResult)
                } else {
                    println("ERROR")
                }
            }

            let entityCompany = NSEntityDescription.entityForName("Company", inManagedObjectContext: context)

            let companyObject = Company(entity: entityCompany!, insertIntoManagedObjectContext: context)

            companyObject.name = company["properties"]["NAME"].string!
            companyObject.employees = employeeObjects


            if !context.save(&error) {
                println("An error occured while saving companies : \(error?.localizedDescription)")
            }

        }
    }


    //==============================================
    // 4)
    //      RESULT
    //==============================================


    let requestCompanies = NSFetchRequest(entityName: "Company")
    let companies = context.executeFetchRequest(requestCompanies, error: nil) as! [Company]
    println("Number of companies : \(companies.count)")

    for company in companies {
        println("\(company.name)")

        for employee in company.employees {
            println("->\(employee.name)")
        }

    }



}

我的数据模型如下所示:

你的关系是多对多的,在你的 Employee managedObject 中你需要有 "companies" 关系而不是属性。那么你必须重新生成你的托管对象子类。

然后在不设置公司关系的情况下创建所有员工实体(使用您的循环)。 然后创建公司,并在为每个公司创建公司时获取员工,然后将他们添加到公司 companyObject.employees = employeeObjects