Update to the question below:
We were able to query the child entity using the Father entity fetch request by using fatherChild relationship. Sample query is as follows:
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:#"fatherChild.name LIKE 'fx'"];
Now what we are trying to do is to use the predicate above but another another condition where we want to find child for a given fathers name. We used the following code
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:#"name LIKE 'john' AND ANY fatherChild.name in 'fx'"];
But the program crashed with the exception:
[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xad49c40
Reading through examples we see we can use subquery but not sure what the syntax would be for our case where we have a entity with one to many relationship. Any help would be appreciated?
Thanks.
Question:
We have a data model with three entities: Father, Mother and Child. Please see the image for reference.
An example Query request we have on the father entity is given below :
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Father"
inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSString *attributeName = #"name";
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:#"%K like %#",
attributeName, searchField.text];
We have similar query request for Mother and Child entities. What we would like to do is to create one query to combine Father and Mother entities. For example we want to search on Father's name =Mike and Mothers name =Jen in a single query. How do we do it?
Thanks for your response.
We found out that we can solve the problem using a SUBQUERY.
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:#"name LIKE %# AND fatherMother.name LIKE %# AND (0!=SUBQUERY(fatherChild,$varChild,$varChild.name=%#).#count)",dad.text, mom.text, baby.text];
Here fatherMother and fatherChild are names of relationships.
dad.text, mom.text and baby.text are UITextField values.
Related
I have a 'position' entity with a to-many relationship to 'employee's. I want to use NSPredicate to do something like this
[position.employees containsObject:meEmployee]
There is a call to perform NSPredicate with block, but sadly it can't be used with CoreData.
How can I do this?
If you are fetching position entities with any employees matching meEmployee then you can use a predicate like this:
NSPredicate *predicate = [NSPredicate predicateWithFornat:#"ANY employees == %#", meEmployee];
But if the inverse relationship for employees is position, you can achieve the same with:
meEmployee.position
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];
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];
I looked at other questions and I saw glimpses of answers but none worked.
Here is my situation, I have two objects "Menu" and "Restaurant". Menu is where I have all menu items, and "Restaurant" is the name of different restaurants. Therefore:
Menu Object
- menuId
- name
- protein
- restaurantID (one-to-one)
Restaurant Object
- restaurantID (one-to-many)
- name
When the user gives me the restaurant name, I want to return the menu items. In SQL, it would be:
SELECT * FROM ZMENU WHERE zrestaurantId = (SELECT Z_PK FROM ZRESTAURANT WHERE zname="Starbucks")
I tried
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:#"Restaurant"];
[request setEntity:e];
*NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(restaurant = (SUBQUERY(Restaurant, $x, $x.name like[cd] %#).#count))", restaurantName];
But I get the error
'NSInvalidArgumentException', reason: 'Unable to generate SQL for predicate (restaurant == SUBQUERY(Restaurant, $x, $x.name LIKE[cd] "Starbucks").#count) (problem on RHS)'
Can someone please help? I want to avoid having to do two fetches (one for restaurant PK, other for the menu item), I believe there should be a more proper solution.
I got it!!!!!
Creating element was correct, just change the subquery to
NSEntityDescription *e = [[model entitiesByName] objectForKey:#"Menu"];
[request setEntity:e];
NSString restaurantName = #"Starbucks";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SUBQUERY(restaurantId, $x, $x.name == %#).#count != 0", restaurantName];
So the way subquery works is - you establish the object you want to get(Select on table) by setting the entity. Then on the predicate you use subquery on the field that references the external table. Then you specify the field in that table that you want to check, and that's it! I have no idea why there is a "count", but it works.
By the way per documentation, inside the subquery you can use %# for ints or strings values you want to check, and you can use %K for field names you want to check. Pretty cool! :-D
I have a Core Data model structure like the following:
Product <-->> OrderProduct where the relationship (from Product to OrderProduct) is called productOrders while the inverse is called product
Order <-->> OrderProduct where the relationship is called orderProducts while the inverse is called order
Client <-->> Order where the relationship is called orders while the inverse is called client
Within an UIViewController I'm using a NSFetchRequest associated with the following predicate:
NSPredicate* predicate = [NSPredicate predicateWithFormat:#"(ANY productOrders.order.client.code == %#)", clientCode];
The predicate works well. I'm able to retrieve products for one (or more) orders that are associated with a specific client.
Now I have to add another step. Find the last order (ordering by date) for a specific client. I've tried the following predicate but it doesn't work:
NSPredicate* predicate = [NSPredicate predicateWithFormat:#"(ANY productOrders.order.client.code == %# AND productOrders.order.#max.orderDate)", clientCode];
where orderDate is of type NSDate.
Do I have to use a SUBQUERY? How can I achieve this? Thank you in advance.
You can use a sortDescriptor on your fetchrequest (fetchrequest takes an array of sortdescriptors).
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"productOrders.order.orderDate" ascending:NO];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
edit: see NeverBe comment
I was able to fix the issue passing the date externally. In other words, I first calculated the last order date for a specific client and then I passed it to the interested element (the one that implement the request).
NSPredicate* predicate = [NSPredicate predicateWithFormat:#"ANY productOrders.order.client.code == %# AND productOrders.order.orderDate == %#", clientCode, lastDate];
I don't know if could be correct but it works.
Hope it helps.