Reputation: 33
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
Upvotes: 1
Views: 425
Reputation: 21536
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 = employee.delivery.allObjects()
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.)
Update
You don't need the thisEmployee
variable. Just change the Employee
variable to be Employee
class:
var employee : Employee!
Then you should be able to set
deliveries = employee.deliveries?.allObjects as! [NSManagedObject]
And in your fetchDelivery()
method, set the predicate with
fetchRequest.predicate = NSPredicate(format:"employee == %@", employee)
(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 the viewDidLoad
method of the DelieveryTableViewController
. If it's nil, there is a problem with passing the value in prepareForSegue
. If not, print employee.deliveries
, etc. Post your results.
Upvotes: 1