List Employees and Tuples
Intermediate Training Core Data
Having created some employees for our companies, we're now ready to fetch these new entities and present them in our list. We'll do this inside of EmployeesController and perform the usual cell registration process to provide the employee's name for each cell. Once we finish this process, we'll look at how we can implement Custom Delegation again to refresh our list after a new employee is created. I implement something called Tuples inside of Swift to return multiple values when creating an Employee. Tuples is really convenient when used properly, here are some additional resources on how they work.

Comments (12)
Farukh
5 years ago
Hi Brian, can you explain me how I delete/edit an employee please? .. excuse for my English and THX in advance :)
Brian Voong
5 years ago
Farukh
5 years ago
I supposed it :) ... Thanks for all you do for us and Have a nice day!
SimonQ
5 years ago
I tried to add the fetch request as `Employee.fetchRequest()` like in an earlier video you did back on doing a batch delete. However, the employees value is set to "Any" for some reason while when I click on the Employee object I can see that it returns a `NSFetchRequest<Employee>(entityName:"Employee")`? Am I missing something here?
Christophe Bugnon
5 years ago
Hey Brian, is it usefull to switch CreateCompany() and SaveCompany() in Coredata or is it better to let them in CreateCompanyController ? Thanks for all. :)
Brian Voong
5 years ago
Christophe Bugnon
5 years ago
Ok I understand. Thanks for your reply. Have a nice day. :)
samposborne
5 years ago
Hey Brian, great tutorials so far! I have made it all the way to the end of this video but can't seem to get my table view to reload the data once saved to core data. When I go back then back into the tableView the data loads. I also have reloadData in my didAddEmployee but seems to have no effect.
Tube
5 years ago
Why keep the table records in memory instead of loading from Core Data as needed?
Tube
5 years ago
You can name the tuple arguments, e.g., func createEmployee(name: String) -> (employee: Employee?, error: Error?) { then, instead of result.1, reference them like this: result.error
bfriendjr
5 years ago
Great Course.... I am trying to build on what I learned and I have a question. I really like how you can have the Tuple in the "CreateEmployeesController" and then pass the new employee info over to the "CoreDataManager" controller for employee creation in Core Data. I implemented an employee image, which is a modified lower quality image much like what was done in the "CreateCompanyController". I can get it to save all the new Employee Info (Including Image) if I have the Save to CoreData function within the "CreateEmplyeesController", but I can not seem to get it to save the image data if I am trying to use the Tuple method. The tuple is working just fine for the normal saving to CoreData for Strings, Ints and so-on. Any guidance would be greatly appreciated.
Brian Voong
5 years ago
bfriendjr
5 years ago
Thanks for the prompt response. I tried adding UIImage, but kept throwing an error. I reapplied that I forgot to import UIKit. After I did that, and a fixed a couple other things, I was able to pull it off. All rookie mistakes. Is it ok if it just pass the image from the "CreateEmployeeController" to the "CoreDataManager" controller, then do the "let imageData = UIImageJPEGRepresentation(itemImage, 0.8)" in the "CreateItem" method of the "CoreDataManager"?
bfriendjr
5 years ago
"Realized" not "Reapplied"
Brian Voong
5 years ago
Gabriel
5 years ago
Which lesson deals with filtering the employees that pertain to a company i.e. all Apple employees?
Brian Voong
5 years ago
Gabriel
5 years ago
Thank you. I am having a real problem understanding how the employees are related to the company ( not the one-to-many relationship) but on Save how the solution knows that this particular entry is an Apple employee rather than any other - is it via the tuple? Thanks again.
seekerlk
4 years ago
I think that is what covered in the next lession.
eliamyro
4 years ago
Hello Brian, instead of using a tuple I used a completion block like this: func createEmployee(name: String, completion: (Employee?, Error?) -> ()) { let context = persistentContainer.viewContext let employee = NSEntityDescription.insertNewObject(forEntityName: "Employee", into: context) as! Employee employee.setValue(name, forKey: "name") do { try context.save() completion(employee, nil) } catch let saveError { completion(nil, saveError) } } Is it a good approach or it is better to return a tuple?
Brian Voong
4 years ago
inode
4 years ago
Thank you Brian for such fantastic course! I have a quick question about fetching all objects into arrays like Employees[] and Companies[]. We do not know how consumers will use our application. What if somebody create a huge amount of companies with a high resolution images. Is it a good idea to fetch all these objects into memory? Or we are fetching only object IDs and they are compact? Is there any other efficient way to work directly with a storage and not load all that stuff into memory? Sorry if the question is stupid, I'm just a newbie :)
Brian Voong
4 years ago
inode
4 years ago
Thank you!
Cinquain
4 years ago
Classic
HELP & SUPPORT