Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8870105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:38:31+00:00 2026-06-14T17:38:31+00:00

I have a model that looks like this: In code, I am enforcing that

  • 0

I have a model that looks like this:

enter image description here

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:38:32+00:00Added an answer on June 14, 2026 at 5:38 pm

    A cheap hack approach:

    add a category method called something like nameOfRelevantB to object A, set myC as a global (or, probably, pass it to a class method on A that stores it to a file-local static) then set nameOfRelevantB as the sectionNameKeyPath on the NSFetchedResultsController.

    nameOfRelevantB would 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 UITableViewDataSource anyway 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 NSFetchedResultsControllerDelegate that breaks things down into sections of its own volition, avoiding the [file static] global.

    A more thorough solution:

    you could override valueForUndefinedKey: on your NSManagedObject subclass and put the relevant C’s objectID directly into the key path. That’s explicitly safe to do because managed object IDs have a URIRepresentation and NSURL provides absoluteString. Each A could then get the string URI from the key path, ask its context’s persistent store coordinator for managedObjectIDForURIRepresentation and then ask the context for existingObjectWithID: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 NSFetchedResultsController directly, giving it a key path from which it can determine sections.

    So, e.g. (typed directly here, untested)

    // get a URI for the relevant myC and prefix it with an '@'
    // so that it's definitely clearly not an ordinary property
    fetchedResults.sectionNameKeyPath = 
            [NSString stringWithFormat:@"@%@", 
                  [[myC.objectID URIRepresentation] absoluteString]];
    
    ... in your subclass for A ...
    
    - (id)valueForUndefinedKey:(NSString *)key
    {
        // check that the key begins with the magic '@' symbol
        if(![key length] || [key characterAtIndex:0] != '@')
            return [super valueForUndefinedKey:key];
    
        // get the original URL
        NSString *URLString = [key substringFromIndex:1];
        NSURL *URL = [NSURL URLWithString:URLString];
    
        // transform the URL into the relevant instance of C
        NSManagedObjectID *objectID = [self.context.persistentStoreCoordinator
                      managedObjectIDForURIRepresentation:URL];
        NSError *error = nil;
        MyCClass *myC = [self.context existingObjectWithID:objectID error:&error];
    
        // check that we got an appropriate instance and didn't
        // generate an error
        if(!myC || error) return [super valueForUndefinedKey:key];
    
        /*
            code here to find the appropriate B and return a suitable title
        */
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object model that looks like this (pseudo code): class Product {
I have my HTML / razor code that looks like this: @model Content.Grid <tr
I have the following code: <li>Reviewed: @Model.Modified</li> and a model that looks like this:
I have a model that looks like this: Performance - Location - Event -
I have a model that looks like this: class Invite(models.Model): user = models.ForeignKey(User) event
I have a view model that looks like this public class ViewModelRound2 { public
I have a model that looks something like this: public abstract class Parent {
I have a class in my domain model root that looks like this: namespace
I have a model that looks something like this class Post(models.Model): id = models.IntegerField(unique=True,
I have an object model that looks like this: public class MyModel { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.