nspredicate for searching entity relationships - ios

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];

Related

Core Data NSPredicate With To One Relationship to Another Entity

I an trying to construct an NSPredicate to search one entity (table) but also search relationships to other entities but am getting an 'unimplemented SQL generation for predicate' error.
The main entity is Files. The Files have a relationship to another entity called Activity. Activity has a string property: name.
However, I cannot get the following NSPredicate to work:
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Files"];
NSPredicate *predActivity = [NSPredicate predicateWithFormat:#"ANY self.activity.name contains[cd] %#",string];
The Files table has a relationship named activity that shows o as the relationship.
I've also tried it without self as in:
NSPredicate *predActivity = [NSPredicate predicateWithFormat:#"ANY activity.name contains[cd] %#",string];
What is the correct way to write this?
Thanks for any suggestions.

Hello, I need to filter a list of contacts but NSPredicate don't work

I need to filter a list of contacts, but not copy that after a certain amount of letters attached filter is damaged sample images and the NSPredicate I'm using.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"CAST(SELF.compositeName, 'NSString') contains[cd] %#",self.SearchNames.text];
Your Predicate syntax is wrong.
let predicate = NSPredicate(format: "SELF.compositeName CONTAINS[cd] %#", searchText)
let filtredArray = NSArray(array: YourContactarray.filteredArrayUsingPredicate(predicate))
When we work with array of dictionary then simply SELF.key is valid for predicate.
After this code reload your table with filtredArray which you can stored in another array for global access. Let me know if you have any problem in this code.
First thing, you have to filter the array using NSPredicate. You can follow the below link for filtering the array.
NSPredicate Example
In that link, there are many examples for filtering array using using NSPredicate.
Here array means, which is used for displaying in UITableView.
Still you have questions, please post the code with array what you have used.
Try this
NSPredicate *predicate = [NSPredicate predicateWithFormat: #"compositeName CONTAINS[c] %#", searchText];

Create a relationship as common in nspredicate

The below predicate is worked for me.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"mcu.family LIKE \"STM32\" AND mcu.serie IN {\"STM32F3\"}"];
How to assign the mcu relationship as common to whole attribute.As like as below,
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"((family LIKE \"STM32\") AND (serie IN {\"STM32F3\"})) _ mcu"];
What can I assign the key in a underscore(_) Place?
Kindly help me.

How to write a Core Data predicate for objects which are related to an object in a to-many relationship

EntityA has a to-many relationship to EntityB (and EntityB has a to-one relationship to EntityA). I have an array of EntityBs (or more accurately, I have an NSArray that contains instances of NSManagedObject which represent EntityB). I want to create an NSFetchRequest that will fetch all EntityAs that have a relationship to at least one of the EntityBs in the array. How do I write the predicate for this fetch request?
The following works, but I think it is sub-optimal; it's hard to grok and I'm sure there must be a better way of expressing this:
NSArray *entityBs = ...;
NSMutableArray *containsEntityBSubPredicates = [NSMutableArray new];
for (NSManagedObject *entityB in entityBs) {
[containsEntityBSubPredicates addObject:[NSPredicate predicateWithFormat:#"%# IN entityBs", entityB]];
}
NSPredicate *containsEntityBsPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:containsEntityBSubPredicates];
I've also tried this, but it doesn't work:
NSArray *entityBs = ...;
NSPredicate *containsEntityBsPredicate = [NSPredicate predicateWithFormat:#"ANY %# in entityBs", entityBs];
Am I missing a simpler solution?
You were almost there with your predicate, just switch the parameters:
[NSPredicate predicateWithFormat:#"ANY entityBs in %#", entityBArray];
Look at Apple's example code with IN here for further explanation.

NSPredicate with SUBQUERY does not work

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];

Resources