views:

101

answers:

3

This has been driving me nuts all day.

I have a weird bug that I think I have narrowed down to an NSPredicate. I have two entities: List and Person. List has a to-many relationship to Person called persons and Person has a to-many relationship to List called lists.

I pass to my a tableview controller a List object. I then want that tableview controller to display the Persons that belong to that list object. I am doing this with a NSFetchedResultsController.

When setting up the NSFRC, I have the following code (memory management omitted for clarity). The List in question is myList:

// Create the request and set it's entity
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

// Create a predicate to get the persons that belong to this list
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY lists == %@)", myList];

// Assign this predicate to the fetch request
[fetchRequest setPredicate:predicate];

// Define some descriptors
NSSortDescriptor *locationDescriptor = [[NSSortDescriptor alloc] initWithKey:@"location" ascending:YES];
NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:locationDescriptor, lastNameDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

// Create and initialize the fetch results controller.
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"location" cacheName:nil];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;

I think the problem is with this line (because it disappears if I remove it):

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY lists == %@)", myList];

What is happening is when the parent view passes myList to the tableview controller, the simulator just hangs. No crash log in the console or anything. It's almost as if it's just taking AGES to sort out the NSFRC.

Is this a problem with the predicate I'm using?

+1  A: 

Do you you need to use NSFetchedResultsController when you can obtain the Persons from the list passed into the tableViewController?

NSSet *people = myList.persons;
falconcreek
You should've edited your original post to ask this.
macatomy
+1  A: 

You are correct, you can just use myList.persons, an NSFetchedResultsController is not necessary in this situation.

macatomy
your comment is confusing. my answer provides an alternative which requires re-design of the tableViewController.
falconcreek
Sorry, I probably misunderstood. But your answer is correct.
macatomy
A: 

Thanks for the suggestions re: using an NSSet. After hours of bug-tracking I realised that the problem lie in my cellForIndexPath method of the table view (so, unrelated to the NSFRC).

Garry