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

时间:2015-09-14 17:03:25

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

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

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

所以,我在做什么:

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)")
        }

    }



}

我的数据模型如下所示:

enter image description here

1 个答案:

答案 0 :(得分:1)

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

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