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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:35:53+00:00 2026-05-22T23:35:53+00:00

Hello Need help with predicate. The problem is that I want to have one

  • 0

Hello Need help with predicate. The problem is that I want to have one function which will fetch from database depends on the multiple values it receives, but the values don’t always exist, does it mean I have to create several different predicates?

Code below

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"brand_id LIKE %@ AND brand_id LIKE %@ AND brand_id LIKE %@ AND brand_id LIKE %@ AND brand_id LIKE %@ AND item_category LIKE %@ AND item_make LIKE %@ AND item_gender LIKE %@ || item_price>%@ || item_price<%@",
                              brand_one, brand_two, brand_three, brand_four, brand_five, categoryString, makeString, genderString,[NSNumber numberWithInt:price_bottom],[NSNumber numberWithInt:price_top]];

brand_one, brand_two and etc sometimes exist and sometimes don’t.

And how should it for item_gender for example. Let’s if there is no gender specified than to have both of them.

Sorry if my description of the problem confusing.

Base on Gobras comments the following code was produces

NSArray *brands = [NSArray arrayWithObjects:brand_one, brand_two, brand_three, brand_four, brand_five, nil];

    NSPredicate *predicate_brands = [NSPredicate predicateWithFormat:@"brand_id like %@" argumentArray:brands];

Logical explanation of what what search function should

  1. fetchrequest
  2. predicate on fetch request

predicate based on 5 brand ids, item id, item category, item make, item gender,
if any of above is empty it should fetch all related entries for example if item category is empty it should fetch all “Jewellery, Watches and etc” but limit it with the rest of the predicate expressions.

Should I create for example compound predicates for brands and than create another one for item category with values “item_category like Jewellery” and “item_category like Watches” and after that how do I exactly bring them together?

  • 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-22T23:35:54+00:00Added an answer on May 22, 2026 at 11:35 pm

    Assemble an appropriate collection of individual predicates into NSCompoundPredicate

    as in

    NSMutableArray *parr = [NSMutableArray array];
    if([brand_one length]) { 
        [parr addObject:[NSPredicate predicateWithFormat:@"brand_id LIKE %@",myBrandId]];
    }
    if([brand_two length]) { 
         // etc 
    }
    
    NSPredicate *compoundpred = [NSCompoundPredicate andPredicateWithSubpredicates:parr];
    

    You can stack up your predicates with orPredicateWithSubpredicates: and andPredicateWithSubpredicates: and NSCompoundPredicate being a NSPredicate itself can be compounded.

    See
    https://developer.apple.com/documentation/foundation/nscompoundpredicate

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

Sidebar

Related Questions

Hello stack overflow I need help with this problem. Ok, I have a flat
Hello I need help with displaying data from 2 table. So i Have 2
Hello I need help with Regular Expression, I want to match each section (number
Hello guys I'm stucked in a point and I need your help. I want
Hello I need some help getting my mysql correct. I need to select one
I need help to solve this problem: I have a csv file like this
I have an XML file which will output a string: <mystring> <manipulate type=caps> <string>Hello
I need help displaying only text from within a file that begins with lets
Hello I need to retrieve data from database via an ajax call. I am
Hello I need to have multiple language support of my django admin application.I can

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.