Employee Information One-to-One Relationship
Intermediate Training Core Data
In order to understand Core Data relationships, it's best to start from the simplest use case of a one-to-one association between two Entities. There are many use cases in the real world that support this type of modeling of objects inside Core Data. In today's lesson, we dive into an example of separating Employee Information into a separate entity, and establishing a one to one relationship with Employee. To further reinforce this concept, let's also create an Address entity that holds onto an employee's address information. Finally, after having finished the creation of these relationships, we go into code to save some dummy information onto our Employee and EmployeeInformation objects.

Comments (9)
codepatrik
5 years ago
Hey Brian, yet another great video
codepatrik
5 years ago
oh my question got cut out in previous comment. I'm wondering if there is any reason to avoid using NSManagedObject subclass initializer like let employee = Employee(context: context)
Brian Voong
5 years ago
lewisle
5 years ago
Hi Brian. I just wonder why do you prefer this ``` employeeInfo.taxId = "456" ``` than this ``` employeeInfo.setValue("456", forKey: "taxId") ``` Though you explained in a previous video that we should use setValue for creation.
Brian Voong
5 years ago
Ashim Dahal
5 years ago
hi brian, I have watched in one of the video tutorial you have shown passing escaping variable for json response with error, similar case here you are passing touple.. which one is more preferable... ?
Brian Voong
5 years ago
K1llarney
5 years ago
Hi Brian, What are the advantages / disadvantages of creating multiple core data entities to seperate attributes. ie: all EmployeeInformation (SSN, BDay, BPlace, TaxID) pertain to each employee, so why seperate them out, away from name? I just ask because while I was learning core data, I had problems understanding one-to-many relationships, so I put all attributes into one entity. This was only because I didn't know any better. It didn't seem to have any adverse effects. Thanks
Brian Voong
5 years ago
salar Soleimani
5 years ago
we have two tables (personel, personeltochart) with one to one relationship (personel.idpersonel, personeltochart.idpersonel). on login event we sync data of personel table from webservice and save it into personel tables. we have to get other table data on another event and save it. the question is when we try to fetch data from these two tables why we get data of personel table correctly but datas of personeltochart table is nil? personel firstname idpersonel personelToChart id idpersonel example of saving data of table let jsonEmza = try jsonDecoder.decode([JSONNameTabel].self, from: data) var privateContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType) privateContext = CoreDataManager.shared.persistentContainer.viewContext jsonEmza.forEach({ (jsonCompany) in let tabel = jsonCompany.tableName if tabel == "Personel"{ jsonCompany.personel?.forEach({ (field) in let personel = Personel(context: privateContext) personel.firstname = field.firstName personel.idpersonel = field.id }) do { try privateContext.save() } catch let saveErr { print("Failed to save personel:", saveErr) } } fetching example let context = CoreDataManager.shared.persistentContainer.viewContext let fetchRequest = NSFetchRequest<Personel>(entityName: "Personel") let predicate = NSPredicate(format: "idpersonel == %@", "147") fetchRequest.predicate = predicate do { let listNameh = try context.fetch(fetchRequest) listNameh.forEach({ (item) in print(item.idpersonel ?? "") print(item.lastname ?? "") print(item.firstname ?? "") print(item.personelToChart?.id) }) } catch let fetchErr { print("Failed to fetch Personel:", fetchErr) } data of item.personelToChart?.id is nil.
Brian Voong
5 years ago
Prabhdeep Singh Randhawa
5 years ago
I like the way u speaks employyyyyeeeeeesssss
magic
5 years ago
Hey brian, i was just wondering if my project was some how big, the coredatamanager class will increase a lot and will be complex to manage, can i use extensions to split it up or is there a better way to do that? Thank you
shubhamsaurav
5 years ago
Hey Brain, I am getting a error when I type out this code. The error says "Value of type 'NSManagedObject' has no member 'employeeInformation'" let employeeInformation = NSEntityDescription.insertNewObject(forEntityName: "EmployeeInformation", into: context) as! EmployeeInformation employeeInformation.taxId = "456" employee.employeeInformation = employeeInformation
shubhamsaurav
5 years ago
I solved the issue. I wasn't casting employee as! Employee.
Brian Voong
5 years ago
HELP & SUPPORT