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 employee
table view. The problem i face is the relationship between the employee
and delivery. Im currently trying to display a specific employees deliveries when clicking on an employee
in the employee
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 UITableview
.
What I'm trying to Accomplish:
1) Display Specific Employee's Deliveries
2) Add deliveries to the NSSet
Here are my two managedObjects
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 employeeTableViewController
to deliveryTableViewContorller
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 deliveryTableViewController
the problem i have is how do i return the amount of deliveries for a specific employee in the numberOfRowsInSection
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.
UPDATE:
Here is the picture of the DelieveryTableViewController (yes i know i spelt delivery wrong)
also need to set the NSPredicate
this is home I'm preparing for segue in EmployeeTableViewController
these are my variables for EmployeeTableViewController
Setting the relationship
With one to many relationships, it is easiest to set the to-one relationship:
Put this line in your
completeOrder
method (you may need to pass theemployee
reference from a previous VC). CoreData will automatically set the inverse relationship for you, addingdelivery
to thedeliveries
set for theemployee
.Showing the Deliveries in a Table View
Having set the relationship, the
deliveries
property ofemployee
contains a set ofDelivery
objects to which it is related. To display these in a table view, create yourdeliveries
array from this set (eg. inviewDidLoad
):Your
numberOfRowsInSection
can then just usedeliveries.count
and thecellForRowAtIndexPath
can usedeliveries[indexPath.row]
to determine whichDelivery
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 youremployee
:Longer term, you should consider using
NSFetchedResultsController
which is designed for displaying CoreData objects in table view.)Update
You don't need the
thisEmployee
variable. Just change theEmployee
variable to beEmployee
class:Then you should be able to set
And in your
fetchDelivery()
method, set the predicate with(after
let fetchRequest = ....
).Update 2
It's difficult to see where the nil value is. To track it down, try printing the value of
employee
in theviewDidLoad
method of theDelieveryTableViewController
. If it's nil, there is a problem with passing the value inprepareForSegue
. If not, printemployee.deliveries
, etc. Post your results.