NSPredicate - fetch entities with to-many relationship - ios

Considering Action with an assignedTo relationship to User entity. (Action.assignedTo->>User)
What would be a query that selects all actions which contain the user in the assignedTo set?

Subquery is a bit heavy handed.
You can use this :
[NSPredicate predicateWithFormat:#"ANY assignedTo.userId == %#", user.userId];

I found that SUBQUERY is the right answer here; it works perfect:
[NSPredicate predicateWithFormat:
#"(SUBQUERY(assignedTo, $a, $a.userId == %ld).#count != 0)", user.userId];
and with filter by status
[NSPredicate predicateWithFormat:
#"(SUBQUERY(assignedTo, $a, $a.userId == %ld).#count != 0) && status != 'Completed'", user.userId];

Did you create your model files using the Xcode generator for CoreData?
If so, check your User.h file, you should see something like this:
#property (nonatomic, retain) NSSet *actions;
That mean that you can call all the actions of that user just calling:
NSSet *userActions = user.actions;
No need to use a predicate.
If still you want to use a predicate, the predicate should look like this:
NSPredicate *userPredicate = [NSPredicate predicateWithFormat:#"assignedTo == %#",user];
If you need to add aditional predicates you should you an array of them a make a NSCompoundPredicate:
NSPredicate *anotherPredicate= ...;
//andPredicateWithSubpredicates if you want the both
//orPredicateWithSubpredicates if you want at least one of them
NSPredicate *compoundPredicate = (NSPredicate*)[NSCompoundPredicate andPredicateWithSubpredicates:#[userPredicate,anotherPrediate]]

Related

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

NSPredicate to-many relationship with inheritance

I'm working on an app where I have Post objects, each Post can have many Entities (mentions, hashtags and links), each Entity have one Post. I have one main class for Entity, then I have three subclasses.
Mention : Entity
My mention class have the following properties:
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSNumber * userId;
I now want to create a NSPredicatethat finds all Posts that are mentioning a certain User and userId, and I'm not sure how I should do that.
I have tried some things like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY mentions.userId like %#", [Session currentUser].userId];
// That doesn't work since Post(s) have many entities and not Mention(s).
// I have also tried something like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY entities.mentions.userId like %#", [Session currentUser].userId];
// And that didn't work either.
Any ideas on how I best should find all posts that are mentioning a certain user with a specific userId?
Inheritance is there so you do not have to code equal attributes twice. Otherwise, managed objects with parent objects are just like other managed objects.
Therefore, you should give your posts three relationships:
Post
hashes Hash
mentions Mention
links Link
Then your problem becomes trivial:
[NSPredicate predicateWithFormat:#"ANY hashes.userID = %#", _currentUser.userID];
Like does not make sense, as userID is presumably unique, and LIKE is much more expensive than simple equivalence.
If you want just one to-many relationship to one class of entity you will have to include another attribute in Entity, e.g. an NSNumber, to indicate what type it is. The predicate would then be as follows, assuming you use an enum to make the number types more readable:
[NSPredicate predicateWithFormat:
#"ANY (entity.type == %# && entity.userID == %#)",
#(EntityTypeMention), _currentUser.userID];

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