views:

26

answers:

1

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

A: 

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)

Person <-->> Worked <<--> Job

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'.

- (void) dumpAllObjects:(NSString *) entityDescription
{
DLog(@"Dump all object of type '%@']", entityDescription);
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:entityDescription
                         inManagedObjectContext:managedObjectContext]];

NSError *error = nil;
NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];

DLog(@" There are %d instances of entity '%@'", [items count], entityDescription);

for (NSManagedObject *managedObject in items)
    {
    if ([managedObject respondsToSelector:@selector(dump)])
        [managedObject dump];
    }
}

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.

westsider
Thanks, westsider. My relationships are bi-directional, as you stated. I was calling removeObject from the parent objects (Person, Job), but the relationship object was still there, albeit without the referenced objects anymore (basically a ghost record in the db).
Rod
I overlooked the delete method of the object context (Core Data is new to me). I was able to do what you said, using a fetchedresultscontroller for the picker data and loading the table data off of the NSSet passed in. I then called delete from object context on the Worked object in the selected row and voila, it all works like a charm now. Thanks so much!
Rod