【发布时间】: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