Reputation: 636
I have to Entity in and both are connected.
I want to insert value in Company entity value but i have no idea how to insert value in company.employees(NSSet).
Both Employees and Company classes are given below:
extension Employees {
@NSManaged var name: String?
@NSManaged var age: NSNumber?
@NSManaged var address: String?
@NSManaged var company: Company?
}
extension Company {
@NSManaged var name: String?
@NSManaged var employees: NSSet?
}
// here i want to insert value
func addEmployees() {
let companyEntity = NSEntityDescription.entityForName("Company", inManagedObjectContext: managedContext)
let company = Company(entity: companyEntity!, insertIntoManagedObjectContext: managedContext)
company.name = txtCompany.text
let employeesEntity = NSEntityDescription.entityForName("Employees", inManagedObjectContext: managedContext)
let employees = Employees(entity: employeesEntity!, insertIntoManagedObjectContext: managedContext)
employees.name = txtName.text
employees.age = Int(txtAge.text!)
employees.address = txtAddress.text
// getting no idea here how to save values to NSSet??
// company.employees =
do {
try managedContext.save()
} catch {
fatalError("Error in saving Data...")
}
}
Upvotes: 2
Views: 4389
Reputation: 1188
I know there are some substantial amount of up votes for jrturton's answer, but as the poser has emphasized it should be done in the other way around. Therefore, let me show how its done.
The Company could have 0 or many employees (ideally 1 or many)
Since NSSet accepts an array as one of its constructers employees should be in a form of an array. Therefore, we gonna create an instance of an array of Employees and append them and then convert it to an NSSet.
func addEmployees() {
let companyEntity = NSEntityDescription.entityForName("Company", inManagedObjectContext: managedContext)
let company = Company(entity: companyEntity!, insertIntoManagedObjectContext: managedContext)
company.name = txtCompany.text
let employeesEntity = NSEntityDescription.entityForName("Employees", inManagedObjectContext: managedContext)
let employees = Employees(entity: employeesEntity!, insertIntoManagedObjectContext: managedContext)
//Add this
var all_employees = [Employees]()
employees.name = txtName.text
employees.age = Int(txtAge.text!)
employees.address = txtAddress.text
//Finally
all_employees.appned(employees)
company.employees = NSSet(array: all_employees!)
do {
try managedContext.save()
} catch {
fatalError("Error in saving Data...")
}
}
Upvotes: 0
Reputation: 119262
It's always easier to do it from the other end:
employees.company = company
Core data will take care of the many side of the relationship because you've specified the inverse relationship in your data model.
Also, rename your entity. It should be called Employee
, since each entity represents one employee. It will save you a lot of confusion down the road.
If you really insist on editing it from the company end, you do it like this:
company.mutableSetValueForKey("employees").addObject(employees)
Which I hope you'll agree is significantly uglier and more prone to errors.
Upvotes: 9