【问题标题】:Core Data one-to-many relationship gets deleted when creating a new oneCore Data 一对多关系在创建新关系时被删除
【发布时间】:2015-09-14 17:03:25
【问题描述】:

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

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

这就是我要做的:

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

2) 我从员工提要中填充核心数据

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

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

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

经过一番研究,我没有发现类似的问题(可能是因为我还是核心数据新手我没有找对地方),但是我找到了这个帖子: 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)")
        }

    }



}

我的数据模型如下所示:

【问题讨论】:

  • 修复你的模型。员工实体应该与您的公司实体具有一对多关系。当关系只在一个方向上定义时,CoreData 不喜欢它。 CoreData 是图表,而不是数据库。
  • 也感谢您的帮助:)

标签: ios swift core-data one-to-many


【解决方案1】:

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

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

【讨论】:

  • 关系应该是前一个的逆向关系,还是单独的关系呢?
  • 反向,因此您可以向员工询问他的公司或公司为其员工
  • 谢谢,我会试试看,然后告诉你进展如何
  • 重新生成类时,我的子类中没有新方法 addEmployees。有什么我想念的吗?
  • 它应该在那里重新生成两个类,有时你需要删除旧的
猜你喜欢
  • 2012-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多