I have the following relationship defined in Core Data
Person –> Worked <– Job
I have a view that shows Person information in a tableview. The user can then click on Worked Items to see all worked items for that person (Worked entity shows hours worked and related job).
I then push a view showing worked jobs for that person.
I also show, in a picker view, a list of jobs that can be added to the Worked list.
I’ve tried to do this every which way, but I’m not sure if I’m going about the right way, so I’d like the experts’ input on this.
What should I pass into the Worked view? I currently pass in the Person object containing the Worked NSSet to load the table view. Then I use a NSFetchedResultsController to load the picker.
So I got the add functionality working, by using the Person and Job addWorkedObject: methods.
But I need to let the user delete a worked item from the table view.
Should I be using two NSFetchedResults? If so, how?
I’m really at my witt’s end with this one, so if anyone can help, I’d really appreciate it.
Thanks,
Rod
I’d need a little more information. First, are you relationships uni-directional as your diagram suggests? From what I’ve read, Core Data is much happier with bidirectional relationships.
For example, this model (below)
states that a Person can ‘have’ many Worked objects, while a Worked object will be associated with at most one Person. Similarly, a Job could be associated with many Worked objects but a Worked object would only ever be associated with at most one Job. Is this your model?
If so, I would use an NSFetchedResultsController for the root view (the one that is showing all Persons). But I would just use NSSets/NSArrays to populate UITableView and UIPickerView.
This is a method that I use for debugging – but it might provide a starting point for getting all Jobs, for example. For your purposes, you could populate an ivar NSArray with the NSManagedObjecs in ‘items’.
Meanwhile, I would subclass UIViewController for your view that shows Worked objects and Job objects – and have that class implement UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate and UIPickerViewDataSource.
As for deleting, I am not sure which aspect is causing problems. If you have -addWorkedObject: methods for Person and Job, then you should also have -reomveWorkedObject: methods. No?
Hope this helps.