I have a model that looks like this:

In code, I am enforcing that although an A can have multiple Bs, it has only one B per C.
What I want to do is list all the As for a given C, grouped by B. Now, this is fairly straight-forward if I start with my C, get the set of all Bs, and then get the set of all As for each B.
What I would like to do is list As using an NSFetchedResultsController. I can filter them by C using “ANY b.c MATCHES myC”, but what I can’t see how to do is to then group them by the correct B.
In other words, since an A has many Bs, how do I figure out which is the one that belongs to my C, and use that in the NSFetchedResultsController? Is that even possible?
A cheap hack approach:
add a category method called something like
nameOfRelevantBto objectA, setmyCas a global (or, probably, pass it to a class method on A that stores it to a file-local static) then setnameOfRelevantBas thesectionNameKeyPathon theNSFetchedResultsController.nameOfRelevantBwould find the B that matches the supplied C and return it.The obvious disadvantage is that you’re reduced to having one relevant C at a time.
You could ameliorate that by instigating a rule that in your app fetched results controllers have a one-to-one relationship with queues or threads and storing the relevant C as queue or thread context but then you’d need to write a manual
UITableViewDataSourceanyway in order to port the results back to the main queue/thread.If you’re going to write a custom data source you might as well make it an
NSFetchedResultsControllerDelegatethat breaks things down into sections of its own volition, avoiding the [file static] global.A more thorough solution:
you could override
valueForUndefinedKey:on yourNSManagedObjectsubclass and put the relevant C’sobjectIDdirectly into the key path. That’s explicitly safe to do because managed object IDs have aURIRepresentationandNSURLprovidesabsoluteString. Each A could then get the string URI from the key path, ask its context’s persistent store coordinator formanagedObjectIDForURIRepresentationand then ask the context forexistingObjectWithID:error:in order to get to the relevant C. Once it has the C it can return the title from the appropriate B.That’d achieve what you want without any type of global state. You’d also be using
NSFetchedResultsControllerdirectly, giving it a key path from which it can determine sections.So, e.g. (typed directly here, untested)
The main caveat is going to be that the object ID, and hence the URI, may change between the initial creation of an object and its first save. Provided you’ve already saved the context, the object ID will stay the same for as long as the object is in the store.