NSPredicate with SUBQUERY does not work - ios

I need to sort my fetch request with selected people, but it doesn't return anything
I tried this code with coredata objects comparing
NSPredicate *peoplePredicate = [NSPredicate predicateWithFormat:#"SUBQUERY(classPeople, $p, $p IN %#).#count == %d", self.selectedPeople, [self.selectedPeople count]];
and this with nsnumber iD comparing
NSPredicate *peoplePredicate = [NSPredicate predicateWithFormat:#"SUBQUERY(classPeople, $p, $p.iD IN %#).#count == %d", self.selectedPeopleiD, [self.selectedPeopleiD count]];
I have entity class, which has relationship people. And I have viewcontroller where I can select some people in list, after selecting people objects are adding to array self.selectedPeople, so I need to fetch all objects of Class entity, which has selected people
thanks in advance

So, then your predicate should look like:
NSPredicate *peoplePredicate = [NSPredicate predicateWithFormat:#"ANY classPeople IN %#", self.selectedPeople];

Related

How to use predicate on object's array of objects values in iOS

I have an Exit object.
Every Exit has an array of Category objects.
Objects look like this :
Category: (id, name)
Exit: (id,name,number,...,NSArray(Categories),...)
I want to use NSPredicate to filter out exits by Category name they have in Category array, but I'm not sure how exactly to write it down.
If I'd wanted to filter out Exits by Exit name, for example, I would do this
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.exitName = %#", name];
NSArray *results = [exits filteredArrayUsingPredicate:predicate];
But I'm not sure how to get into the Categories array and search by Category Name.
Any suggestions ?
Try this
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY SELF.exitName.category_name CONTAINS[c] %#", name];
NSArray *results = [exits filteredArrayUsingPredicate:predicate];
Try this predicate
NSPredicate *matchingCategory = [NSPredicate predicateWithFormat:#"SUBQUERY(categories, $c, name CONTAINS[cd] %#).#count > 0", categoryName];
Explanation
The SUBQUERY() function creates an array of objects which match the given sub-predicate. In this case, that sub-predicate is name CONTAINS[cd] %#. The way it works is by iterating over the collection (first param), assigning the name $c (second param) to refer to each element and then testing to see if the sub-predicate matches that element. If it matches, it's added to the array.
After the SUBQUERY() completes, the array is aggregated using the #count property. If there were any matches (#count > 0), the overall predicate on the exits will match the exit being tested.

NSNumber property of NSManagedObject with NSPredicate

Here I have attribute (name :- universal_Id) in core data as NSNumber in modal class
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"universal_Id == %d",0];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"universal_Id == %d",[NSNumber numberWithInt:0]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"universal_Id == %#",[NSNumber numberWithInt:0]];
Please provide me some correct solution for above predicates. Which one is the correct way to fetch data on the bases of universal_Id?
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"universal_Id == %d",0];
is correct because Core Data is ORM and uses objects to represent scalar values. So when you need to store data in core data , you need to use objects. In predicate building, here you need not to use objects, scalar value is enough to build a predicate, because at the end, core data query matches the universal_Id value, not the object.
Update
you can also use
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.universal_Id == %d",0];

Core Data Predicate - Check if any element in array matches any element in another array

I'm trying to use a predicate to filter objects where an intersection exists between two arrays.
The NSManagedObject has an array(Of Strings) attribute named "transmissions". And there is another array(Of Strings) that will contain words to filter by, named "filters".
I'm not sure how to find if any elements in "transmissions" match any element in "filters".
I've tried
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY SELF.transmission in[c] %#",transmissions];
or
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY transmission in[c] %#",transmissions];
However, core data fetches no results where there should be some.
Try this.
NSPredicate *predicate = nil;
NSMutableArray *predicates = [NSMutableArray new];
for (NSString *transmission in transmissions) {
[predicates addObject:[NSPredicate predicateWithFormat:#"transmission == %#", transmission]];
}
if (predicates.count > 0)
predicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicates];
// make fetch request using 'predicate'
You can use the keyword CONTAINS to check an object exists in a collection.
[NSPredicate predicateWithFormat:#"favouriteFilms CONTAINS %#", film]
I believe the same thing can be achieved with IN by switching the LHS and RHS of the predicate. I have only successfully implemented this functionality using CONTAINS however.

NSPredicate too many arguments. Hard to read it

I just faced with some issue that is connected to clear code. I use predicates in many modules of my projects. When I work with core data the NSPredicates are really helpful for retrieving filtered object.
But the predicating is good for logic but not for code readability as I use it.
So let's see on this example:
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"(ANY option.playerzoneID == %d) \
AND (ANY option.gameId == %#) \
AND (team.teamID == %#) \
AND (league.leagueID == %#) \",
zoneIdType,
SELECTED_GAME.gameID,
SELECTED_TEAM.teamID,
SELECTED_LEAGUE.leagueID];
When I look on this predicate I am little confused about this structure even in case that this predicate I have written one week ago.
Any suggestion how to make this code more readable?
I think it will be better to have something like this:
[predicate setParametr:#"gameID == %#", SELECTED_GAME.gameID];
[predicate setParametr:#"league.leagueID == %#", SELECTED_LEAGUE.leagueID];
You can explore the following:
+ (NSPredicate *)andPredicateWithSubpredicates:(NSArray *)subpredicates
+ (NSPredicate *)orPredicateWithSubpredicates:(NSArray *)subpredicates
+ (NSPredicate *)notPredicateWithSubpredicate:(NSArray *)subpredicate
With this you can actually form short predicates and join them together with arrays. So, you don't exactly get what you wished for, but its a near shot.
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:#"(ANY option.playerzoneID == %d)", zoneIdType];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:#"(ANY option.gameId == %d)", SELECTED_GAME.gameID];
NSPredicate *predicate3 = [NSPredicate predicateWithFormat:#"(ANY team.teamID == %#)", SELECTED_TEAM.teamID];
NSCompoundPredicate *compoundANDPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:#[predicate1, predicate2, predicate3]];
Read more about these here:http://nshipster.com/nspredicate/
Edit: Using OR Predicates along with AND Predicates:
Let's assume: you have this predicate to add as OR:
NSPredicate *predicate4 = [NSPredicate predicateWithFormat:#"(ANY team.teamName == %#)", SELECTED_TEAM.teamName];
So, you can group your AND and OR predicates into one compound predicate (compoundANDPredicate as shown above) and then use
+ (NSPredicate *)orPredicateWithSubpredicates:(NSArray *)subpredicates
So it becomes:
[NSCompoundPredicate orPredicateWithSubpredicates:#[compoundANDPredicate, predicate4]];

nspredicate for searching entity relationships

I wrote the following predicate for the following structure, not sure if its working correctly:
I have a Car.h managedobject
Car.h - name,
make,
model.
toCarParts relationship - CarPart.h - wheel,
tire,
mirror,
etc...
i have an array with many Car.h objects
I want to find only those that have a tire (so i have to look through toCarParts at all objects, and if any of the CarParts match my query, I need to pull that car into a results array)
will this predicate do this?
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY toCarParts.name == [c] %#", carPartString];
Thank you in advance
I believe this should work for you.
NSPredicate *predicate =
[NSPredicate predicateWithFormat:#"ANY toCarParts.name like %#", carPartString];

Resources