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 6112181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:42:55+00:00 2026-05-23T14:42:55+00:00

Given the following contrived example: I would like to query my data for all

  • 0

Given the following contrived example:

Core Data entity diagram for my Awesome Pet Shop App (patent pending)

I would like to query my data for all objects that are either cats or dogs. I want the result set ordered by name regardless of species, so fetching all cats then fetching all dogs won’t do. I want to do this in a single query.

One way to do this would be to add a petType field to Pet, give every record a petType value that identifies the sub-entity it belongs to, then query like so:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Pet" 
                                          inManagedObjectContext:myMOC];
[fetchRequest setEntity:entity];

// petType values: 1 = dog, 2 = cat, 3 = goldfish. Yuk.
NSPredicate *p = [NSPredicate predicateWithFormat:@"petType = 1 OR petType = 2"]
[fetchRequest setPredicate:p];

// etc...

But the mere thought of doing it that way makes me shudder. Is there a better way?


Update: Thanks to all those who’ve replied – there are some really good, well-thought out solutions here and I appreciate all of them.

To give this some context, the real data model is a little more complex than this (aren’t they always), but it’s pretty well organised. I’ve designed more than my fair share of data schemas in my time and I’m happy that the entities and their relationships are well considered. This issue has come about because (to extend the already shaky contrived example) the client originally wanted:

  • a view showing a list of all pets
  • a view showing a list of goldfish
  • a view showing a list of cats
  • a view showing a list of dogs

So far, so good. But they also want a view showing a combined list of all cats and dogs “because little girls like cats and dogs”. (Initially it was cats and goldfish, for the same reason.) There isn’t really a way to naturally group that subset of the concrete entities; it’s really rather arbitrary.

So far, Dave Dribin’s “abstract intermediate entity” approach seems like the cleanest solution, although in my case I think it would feel somewhat artificial; really the only way you could truthfully label the intermediate entity would be as “ThingLittleGirlsLike”! 🙂

  • 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-05-23T14:42:56+00:00Added an answer on May 23, 2026 at 2:42 pm

    As you’ve found out, you cannot use entity.name in your fetch predicate for SQLite stores (you can use it on other store types). You can add a petType, as you suggest, which works well enough, but makes me shudder, too. As an alternative, you could fetch all Pets, and then filter the fetched results based on entity.name. But that’s a bit inefficient, since you’re loading more entities than you need and your doing in-memory filtering that SQLite could be doing on your behalf.

    I think the real question is: what are you trying to do? If you really need to fetch Cats and Dogs without Goldfish, then your model should reflect that. You could insert a FourLeggedPet abstract entity as a parent of Cat and Dog. Then, in your fetch request, use FourLeggedPet as the entity with setIncludesSubentities:YES.

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

Sidebar

Related Questions

Given the following code (it's a macro that generates code for a list data
Given the following contrived example code: struct abc { int x[5]; int y[5]; };
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(
Given the following: List<List<Option>> optionLists; what would be a quick way to determine the
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
Given the following: &row->count Would &(row->count) be evaluated or (&row)->count be evaluated in C++?
Given the following code (that doesn't work): while True: # Snip: print out current
Is there a proper term in data modeling to describe the following? Given table
Given the following assemblage of classes (contrived): public class School { [PrimaryKey] public string
Given the following LINQ to SQL query: var test = from i in Imports

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.