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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:16:47+00:00 2026-06-02T20:16:47+00:00

Back again needing more help with constructing my NSPredicates :( Category { name:string subs<–>>SubCategory

  • 0

Back again needing more help with constructing my NSPredicates 🙁

Category
{
   name:string
   subs<-->>SubCategory
}

SubCategory
{
   name:string
   numbervalue:NSNumber
}

I would like to know how to create a predicate with AND and OR.

For example, I would like to return every Category with name == “category_name” that also has a subcategory with a numbervalue of “valueA” OR “valueB”.

I’ve tried every possible combination of predicate constructors I could come up with but I just cant get it to work.

This is my best attempt so far.

NSPredicate *predicate=[NSPredicate predicateWithFormat@"(name== %@) AND (ANY subs.numbervalue==%@ OR ANY subs.numbervalue==%@)", @"aname", value1, value2];

Edit 1

Second attempt. The filtering on the ‘numbervalue’ is still not working.

NSNumber valueA=[NSNumber numberWithInt:20];
NSNumber valueA=[NSNumber numberWithInt:90];
NSArray *arr=[NSArray arrayWithObject:valueA,valueB,nil];

predicate=[NSPredicate predicateWithFormat:@"(name==%@)AND(ANY subs.numbervalue IN %@)",@"aname",arr];

Edit 2

I’ve tried filtering just by numberValue and have left the name out alltogether.

1) using this results in the entire set being returned even if only 1 item in the set has the value.

predicate=[NSPredicate predicateWithFormat:@"(ANY subs.numbervalue IN %@)",arr];

2) Using this results in the same problem, every item in the set being returned even if only 1 of them matches.

predicate=[NSPredicate predicateWithFormat:@"((SUBQUERY(subs, $x, $x.numbervalue == %@ or $x.numbervalue == %@).@count > 0)", value1, value2];

Also using the simplest version results in the same problem…. I didnt notice this earlier.

NSPredicate *predicate=[NSPredicate predicateWithFormat@"(ANY subs.numbervalue==%@ ,valueA];

So I think I have my problem narrowed down.

Category is an Entity.
SubCategory is an Entity.

Category has a one to many relationship with SubCategory and is represented as an NSSet of SubCategory.

Each SubCategory has an attribute named numbervalue.

Edit 3

To test I have 2 Categorys.
Both Categories have 10 subs, each with a numbervalue of 1 -10;

Using this code both categories are returned along with all 10 subs for each.

Expected result is both categories returned , each with just 2 subs.

I’ve traced out all my objects and the data is correct. The problem is I cant return a category that has filtered subs.

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *context=[[DataSource sharedDataSource]managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

[fetchRequest setFetchBatchSize:20];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

NSPredicate *predicate;
NSNumber *a=[NSNumber numberWithFloat:1];
NSNumber *b=[NSNumber numberWithFloat:2];
predicate=[NSPredicate predicateWithFormat:@"(SUBQUERY(subs,$x,$x.numbervalue==%@ or $x.numbervalue==%@).@count> 0)",a,b];

[fetchRequest setPredicate:predicate];
[fetchRequest setSortDescriptors:sortDescriptors];

NSError *error = nil;
[NSFetchedResultsContoller deleteCacheWithName:nil];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@"name" cacheName:@"Master"];
aFetchedResultsController.delegate = self;

self.fetchedResultsController = aFetchedResultsController;

if (![self.fetchedResultsController performFetch:&error]) {

   NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
   abort();
}
  • 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-02T20:16:49+00:00Added an answer on June 2, 2026 at 8:16 pm

    I’ll do the following using a SUBQUERY.

    [NSPredicate predicateWithFormat@"(name == %@) AND (SUBQUERY(subs, $x, $x.numbervalue == %@ or $x.numbervalue == %@).@count > 0)", @"aname", value1, value2];
    

    Try it and let me know.

    Another solution could be to grab the whole collection subs and then filter the retrieved set with a predicate to check if elements match for value1 or value2.

    Hope it helps.

    Edit

    If you follow Eimantas suggestion make to sure to create the right array:

    NSNumber valueA = [NSNumber numberWithInt:20];
    NSNumber valueB = [NSNumber numberWithInt:90];
    NSArray *arr = [NSArray arrayWithObjects:valueA, valueB, nil];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

back again with some more SQLAlchemy shenanigans. Let me step through this. My table
I'm back again to show my ignorance once more. I need assistance grabbing numbers
Back again with another Flex question. I have an XML structure like... <Student> <Name>X</Name>
Im coming back to needing to use SQL again after not using it for
I want to convert a time_t to a string and back again. I'd like
Back again, thanks for all the help last time. I'm running this query: $query
Back again with a javascript question within sharepoint. I would like to hide a
Back in my C/C++ days, coding an infinite loop as while (true) felt more
I find myself needing to call back into the JVM from arbitrary native threads
I am getting back again and again to it thinking about the best way

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.