NSPredicate ANY with multiple columns - ios

I've 2 tables Person and Social.
Person has multiple Socials.
Socials has username and Service.
I'm trying to write a predicate on Person table which will match the people with proper username and service from the Social table. The statement below is wrong. Please let me know if there is a proper syntax to achive the same or if there is any better method to achieve the same.
[predicates addObject:[NSPredicate predicateWithFormat:#" ANY (socials.username = %#
AND socials.service = %#)",#"username",#"service"]];

That is a typical use-case for a SUBQUERY:
[NSPredicate predicateWithFormat:#"SUBQUERY(socials, $s, $s.username = %# AND $s.service = %#).#count > 0",
#"username", #"service"]

Related

NSPredicate to query many-to-many relationships

I have two Core Data classes that are related to each other with many-to-many relation. Let the classes be Item and Group, where Item.groups can reference to multiple Group, and Group.items to multiple Item objects.
I am trying to use NSPredicate to query all Item objects that do not belong to a specific Group. Using [NSPredicate predicateWithFormat:#"ANY group == %#", specificGroup] to query Item objects I can get the inverse set of what I want. It has proven hard to accomplish the opposite.
What I have tried and found not working:
[NSPredicate predicateWithFormat:#"NONE groups == %#", specificGroup] // always returns 0 results
[NSPredicate predicateWithFormat:#"NOT (ANY groups == %#)", specificGroup] // always returns 0 results
[NSPredicate predicateWithFormat:#"ANY groups != %#", specificGroup] // always returns 0 results
Any help much appreciated.

Filtering NSArray of NSmanagedObject with one-to-may relationship

I have 3 entities, "Bar", "Waiter", "Product" the relationship is as follows,
A "Bar" has one to many relationship with "Waiter" and the same from Waiter to Product.
I have an array of Waiters working on a Bar, and I want to filter the waiters based on if they served a certain Product.
Im trying to filter the array of all the Waiters in a bar, [bar.toWaiter allObjects]
So far I've tried:
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:#"toProduct.endSaleDate > %# AND toProduct.published == YES", [NSDate date]];
and
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:#"ANY toProduct.endSaleDate > %# AND ANY toProduct.published == YES", [NSDate date]];
I only want those waiters who have a Product that has "published" to YES and endSaleDate in the future. What Im getting is Waiters that have either a Product published or a product with a future date.
How can I achieve what Im looking for.
Thanks!
It seems that the predicate is interpreted as "give me waiters which have ANY published product and ANY product with a future date". But it does not require that the same product fulfils both conditions.
You can use a SUBQUERY to ensure that:
[NSPredicate predicateWithFormat:"#"SUBQUERY(toProduct, $product, $product.published == 1 AND $product.endSaleDate > %#).#count > 0"];

Data Retrieval With three entities without using manual looping

I have three entities.
I need to retrieve all the lectures that teach a certain student.
So far the solution i was able to come up is
first to use a subquery to retrieve all the courses the student takes by
NSPredicate *predicate = [NSPredicate
predicateWithFormat:#"SUBQUERY(students, $student, $ student.name ==
'student two').#count > 0"];
I execute the fetched request to obtain fetchedObjects.
Next i compare all the lecture objects with fetchedObjects. The lecture who conducts a course in fetchedObjects is the person who teaches the student in question.
Is there a much neater method of doing this without doing a comparison by hand ?
I mean can i do it using predicates alone?
To fetch all lecturers that have a course with the given student, use the predicate
[NSPredicate predicateWithFormat:#"SUBQUERY(courses, $c, ANY $c.students.name == %#).#count > 0", studentName]
Remark: In your first example (fetch all courses for a student), you don't need a SUBQUERY:
[NSPredicate predicateWithFormat:#"ANY students.name == %#", studentName]
Do you want to retrieve "Course" entity or "Lecturer" entities ?
for Course you could try this predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"students.name = %#",NAME];
now you have Course entities and you can get Lecturers too,
for Lecturers you could try
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY courses.#unionOfArrays.students.name = %#",NAME];

CoreData - Fetch request with relationship object

please confirm if i understand that right...
Imagine there is an entity 'person' and an entity 'credit_card'.
So one 'person' can have many 'credit_card's.
The person entity has the attributes: name: STRING and age: INT
and relationship: creditcards (to many) inverse
and the credit card entity has: card_number: INT and valid_date: DATE and relationship: card_user (to one) inverse
In my code i have a specific person (ManagedObject) called f.e. Person *currentUser. If i now want to get all credit cards of this specific person with a specific 'valid_date' i would create a fetch request (for Entity 'credit_card') with following predicates:
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:
#"valid_date == <NSDate object>"];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:
#"ANY card_user like currentUser"];
This predicate works well for me, but is that the right way? Should i really ask for: "ANY relationship name like ManagedObject" ?
If I'm understanding what you want correctly, all you need to do is use the creditcards property on your Person *currentUser and filter it down:
NSSet *setOfCreditCards = currentUser.creditcards;
NSPredicate *filter = [NSPredicate predicateWithFormat: #"valid_date == %#", date];
NSSet *cardsWithValidDates = [setOfCreditCards filteredSetUsingPredicate:filter];
The reason you tell CoreData about relationships is to avoid making another query from scratch.
You do not need to use the ANY key word if you have set up you core data model correctly. Specifically Credit cards need to have a person relationship back to the person object (you should get a warning if you didn't do this). Then you could combine both predicates into one like this
NSPredicate* predicate = [NSPredicate predicateWithFormat:#"person == %# AND valid_date == %#", currentUser, valid_date];

NSPredicate for a Core Data Search

I have 3 NSManagedObjets; Person, Stuff, and Collection.
I want to use a NSPredicate to get a list of all Collections that ThePerson has.
Example: Scott has objectA and objectB which are in collection Letters and object1 which is in collection Numbers.
I want to be able to do a fetch request and get back collection Letters and Numbers.
I tried:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY stuffs.persons == %#", person];
And:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SUBQUERY(stuffs, $s, ANY $s.persons == %#)", scott];
Any suggestions?
Your SUBQUERY syntax is wrong (for a full explanation, see this answer or this answer). It should be something like:
SUBQUERY(stuffs, $s, ANY $s.persons == %#).#count > 0
Since it seems that you already have a reference to a ThePerson object, you don't need to do a fetch or use a predicate. You can traverse the relationships you've declared to get the collections. You can get all of the Collections that ThePerson has by using:
NSSet *collections = [person valueForKeyPath:#"stuffs.collections"];

Resources