fizzy fizzy - 1 year ago 64
Swift Question

Core Data One-To-Many Swift

The question i have is in regards to a

Core Data
one-to-many relationship
as of right now i have my app being able to let the user input employee information and saving to
core data
, then updating the
table view. The problem i face is the relationship between the
and delivery. Im currently trying to display a specific employees deliveries when clicking on an
in the
table view. After selecting an employee from the employee tableView i want it to segue to another tableview and display the employees deliveries in another

What I'm trying to Accomplish:

1) Display Specific Employee's Deliveries

2) Add deliveries to the NSSet

Here are my two

extension Delievery {

@NSManaged var address: String?
@NSManaged var phoneNumber: String?
@NSManaged var subTotal: NSNumber?
@NSManaged var total: NSNumber?
@NSManaged var employee: Employee?


extension Employee {

@NSManaged var first: String?
@NSManaged var last: String?
@NSManaged var phoneNumber: String?
@NSManaged var wage: NSNumber?
@NSManaged var id: NSNumber?
@NSManaged var delievery: NSSet?


how i prepared for segue from

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue == "DelieverySegue"{
let employeeDeliveries = segue.destinationViewController as! DelieveryTableViewController

let indexPath = self.tableView.indexPathForSelectedRow

let selectedEmployee = employees[indexPath!.row]

employeeDeliveries.employee = selectedEmployee


The variables of the deliveryTableViewController are

var employee: NSManagedObject!

var deliveries : [NSManagedObject]!

var managedObjectContext : NSManagedObjectContext!

In this photo it shows the rest of my
the problem i have is how do i return the amount of deliveries for a specific employee in the
function and how do i fetch the deliveries of the employee.
deliveryTableViewController set up

In this last photo my question is how to i add this delivery entry to the employee selected? this is how I'm currently saving my delivery entries
how i save my delivery entries

If you made it this far i appreciate you looking through my problem. If anyone can help me the slightest with this issue i'd appreciate it if you feel i've left some information out that is needed please let me know.


Here is the picture of the DelieveryTableViewController (yes i know i spelt delivery wrong)

Picture of DelieveryTableViewController with viewDidLoad()

also need to set the NSPredicate

Picture of DeliveryTableViewController fetchRequest function

Answer Source

Setting the relationship

With one to many relationships, it is easiest to set the to-one relationship:

delivery.employee = employee

Put this line in your completeOrder method (you may need to pass the employee reference from a previous VC). CoreData will automatically set the inverse relationship for you, adding delivery to the deliveries set for the employee.

Showing the Deliveries in a Table View

Having set the relationship, the deliveries property of employee contains a set of Delivery objects to which it is related. To display these in a table view, create your deliveries array from this set (eg. in viewDidLoad):

deliveries =

Your numberOfRowsInSection can then just use deliveries.count and the cellForRowAtIndexPath can use deliveries[indexPath.row] to determine which Delivery to display in each cell.

(An alternative is to fetch the deliveries array in the normal way, but to use a predicate to restrict the results to those that are related to your employee:

fetch.predicate = NSPredicate(format:"employee == %@", employee)

Longer term, you should consider using NSFetchedResultsController which is designed for displaying CoreData objects in table view.)