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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:31:03+00:00 2026-06-16T22:31:03+00:00

The problem is that I don’t know how can I make a NSPredicate that

  • 0

The problem is that I don’t know how can I make a NSPredicate that looks in two Entities. Any ideas?

Let’s assume I have 2 Entities:

Library:
1 lib_id
2 name
3 address

and:

Books
1 book_id
2 book_name
3 book_author
4 lib_id

There isn’t any Core Data Relation between them. I want to make a fetch request that will return an NSArray with all the libraries that contain at least one book.

I assume it’s something like this:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescriptor *entity = [NSEntityDescriptor entityForName:@"Books" inManagedObjectContext:context];
 // some NSPredicate...
array = [context executeFetch ...];
  • 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-16T22:31:05+00:00Added an answer on June 16, 2026 at 10:31 pm

    It’s more complicated than you think, but it’s not impossible.

    Your data model’s use of lib_id on both entities, and your comment that you can’t add a Core Data relationship between the entities, suggests you’re falling into a classic trap: You’re treating Core Data like a relational database. Core Data’s API is not designed like a relational database. You can make it work that way, but you should be aware that you are going out of your way to make Core Data more difficult than necessary. If the solution below looks convoluted, it’s because you’re doing Core Data wrong.

    If you were using Core Data as it’s intended to be used– in this case with a relationship between the two entities– the lookup would be simple. You could just do a fetch for every Library with no entries in its books relationship.

    With that said, you solve the immediate problem in two steps.

    First, get every unique value of lib_id that’s used on instances of Books. The following will get you an NSArray containing strings that match the unique values:

    NSFetchRequest *bookRequest = [NSFetchRequest fetchRequestWithEntityName:@"Books"];
    [bookRequest setPropertiesToFetch:@[@"lib_id"]];
    [bookRequest setResultType:NSDictionaryResultType];
    [bookRequest setReturnsDistinctResults:YES];
    
    NSError *error = nil;
    NSArray *libIdsInBooksDicts = [[self managedObjectContext] executeFetchRequest:bookRequest error:&error];
    if (libIdsInBooksDicts == nil) {
        NSLog(@"Fetch error: %@", error);
        return;
    }
    
    NSArray *libIdsInBooks = [libIdsInBooksDicts valueForKey:@"lib_id"];
    

    The last line above is there because libIdsInBooksDicts actually contains an array of dictionaries, and each dictionary in turn has a key named lib_id and a value that’s the actual ID. You just want the values.

    Next, look up every Library whose lib_id is in the list you just got:

    NSFetchRequest *libRequest = [NSFetchRequest fetchRequestWithEntityName:@"Library"];
    [libRequest setPredicate:[NSPredicate predicateWithFormat:@"lib_id in %@", libIdsInBooks]];
    
    error = nil;
    NSArray *librariesWithAtLeastOneBook = [[self managedObjectContext] executeFetchRequest:libRequest error:&error];
    if (librariesWithAtLeastOneBook == nil) {
        NSLog(@"Fetch error: %@", error);
        return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have two concave 2d polygons(A, B), that don't intersect. The problem
I have a problem that I don't know how to solve. Let me explain
So I have a problem that I don't really know how to go about.
i have a problem, that i don't know how to solve it. i have
I have a problem that I don't know how to solve. I have have
I'm facing a recurrent problem that I don't know why it is happening. It
Quick question, I have a simple javascript problem that I don't know the fix
I got a problem that I don't know how to fix it. Please help!
I'm facing a problem that I don't know how to solve and am hoping
I am having a strange problem that I don't understand, and therefore don't know

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.