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

  • SEARCH
  • Home
  • 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 8445153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:35:18+00:00 2026-06-10T09:35:18+00:00

I have the following model graph: +———–+ +———–+ | Container | | Group |

  • 0

I have the following model graph:

                  +-----------+     +-----------+
                  | Container |     |   Group   |
                  +-----------+     +-----------+
                  |  groups   |<-->>| container |
                  +-----------+     +-----------+
                        ^                 ^
                        |                 |
+-----------+     +-----------+     +-----------+     +---------+
|   Owner   |     |  ToyBox   |     |  ToyType  |     |  Item   |
+-----------+     +-----------+     +-----------+     +---------+
|  toyBox   |<--->|   owner   |     |   items   |<-->>| toyType |
+-----------+     +-----------+     +-----------+     +---------+

In a UITableView I am to display a list a list of Items. In this case I only want to show the items that belong to a particular owner. To do this I will use NSFetchedResultsController to display the items. This means that I need create an NSFetchRequest with an appropriate NSPredicate to give to the NSFetchedResultsController.

Attempting to use a keypath predicate causes an exception due to the parent entities. This appears to be an Apple bug or decision not to support. A radar has been filed. Additionally, I do not wish to flatten the entities.

So that left me with an attempt to do this with SUBQUERY() as follows:

NSFetchRequest *itemsFetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Item"];
NSPredicate *itemsPredicate = [NSPredicate predicateWithFormat:@"SUBQUERY(toyItem, $g, SUBQUERY($g.container, $c, SUBQUERY($c.owner, $o, $o = %@).@count > 0).@count > 0).@count > 0", ownerObject];

This results in the following exception:

Can't have a non-relationship collection element in a subquerySUBQUERY($c.owner, $o, $o == <MLMOOwner: ...

I realize that because the relationship is one to one between Owner and ToyBox that there isn’t a collection returned and this is the problem. So my questions are:

1) Is there a way to force the return of a collection for to-one relationships in the subquery?
2) If not is there another way to go about creating this predicate for the fetch request?

  • 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-10T09:35:19+00:00Added an answer on June 10, 2026 at 9:35 am

    Seems like it should be a lot simpler to do what you want. If you want all the items with a given owner, start with the owner and use a key path to get the associated items. You don’t need a predicate. If you’ve got 10 jars of peanuts and you want to retrieve the peanuts in jar 2, you don’t start with the set of all peanuts and then filter them according to their jar, right? You first get jar 2, and then you look at the peanuts it contains. So do this:

    NSSet *groups = [ownerObject valueForKeyPath:@"toyBox.groups"];
    

    That gives you all the groups owned by ownerObject. You can’t just get all the items using a single key path because the Group entity doesn’t have an items attribute. You could make life easier for yourself by giving Group an items accessor, even if it only returns an empty set. That would let you do this:

    NSSet *items = [ownerObject valueForKeyPath:@"toyBox.groups.items"];
    

    If you don’t want to add an items attribute to Group, you’ll have to filter the set groups from the first example to pick out just those objects that are ToyType objects. You can then use the key path @"items" on that set to get the list of items that you want.

    You have to be a bit careful with entity inheritance. You’ve just seen how it can make fetching a little more complicated. It also has implications for the way your data is stored, namely that all instances of sub-entities are stored in the same table. So, if you have a dozen entities derived from Group (such as ToyType), all the instances of all those entities are stored together.

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

Sidebar

Related Questions

I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
I have the following model: public class Contact { public Contact() { Name =
I have the following model: public class Tag { public int Id { get;
I have the following model: WebPromocion: connection: doctrine tableName: WebPromocion columns: id: type: integer(4)
I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
I have the following model: class User: name = models.CharField( max_length = 200 )
I have the following model association: a student model and has_many scores. I need
I have the following model setup - a User is interested in projects in
I have the following Model and Controller files, and when i visit this url,

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.