iOS Predicate for RLMLinkingObjects (Realm) - ios

I have linking object in my category.
#property (readonly) RLMLinkingObjects *relatedAttachments;
I only want to take category if it have relatedAttachments. I try with this.
NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"relatedAttachments.count > 0"]];
self.categories = [[Category objectsWithPredicate:pred] sortedResultsUsingProperty:#"id" ascending:YES];
I got error like this.
RLMPrecondition(property, #"Invalid property name",
So I try with other.
NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"relatedAttachments.count!=nil"]];
NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"relatedAttachments.count!=NULL"]];
But it is still not okay. How shall I do?

You're looking for a predicate like:
[NSPredicate predicateWithFormat:#"relatedAttachments.#count > 0"];
#count is a collection operator that evaluates to the number of elements in the collection it appears to the right of.

Related

How to filter model by its property?

I have a modelArr:
NSArray<MyModel *> *modelArr = xxx;
And in the MyModel, has property, such as type.
How can I filter the modelArr to get a new Array only have the same type?
You can use NSPredicate for that.
Compare type property with specific value.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"type = %#", searchType];
NSArray *filterArray = [modelArr filteredArrayUsingPredicate:predicate];
If you want to check type property contains specific value than
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"type contains[cd] %#", searchType];
NSArray *filterArray = [modelArr filteredArrayUsingPredicate:predicate];
You can use NSPredicate, Please use this code
NSPredicate *bPredicate = [NSPredicate predicateWithFormat:#"SELF.name contains[cd] %#",self.searchText.text];
self.filteredArray = [self.hotelArray filteredArrayUsingPredicate:bPredicate];
NSLog(#"HERE %#",self.filteredArray);`
Try this
NSArray *uniqueNames = [modelArr valueForKeyPath:#"#distinctUnionOfObjects.type"];

An array of id to filter an Array via NSPredicate

The following predicate works if I only pass a single itemId but I wonder what if I have multiple itemId s, how could I able to make it work ?
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(%K == %#)", kItemId, itemId];
NSArray *filteredArray = [restaurantData.itemArray filteredArrayUsingPredicate:predicate];
You can do IN query:
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"(%K IN %#)", kItemId, yourArrayOfItemId];

NSPredicate with ANY and "=nil" doesn't work

This condition works
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"ANY region.beacons.major = %d",rangedBeacon.major.intValue];
But this not
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"ANY region.beacons.minor = nil"];
beacons is a list inside region object. Major and minor have type NSNumber
You may try this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY region.beacons.minor = $minor"];
predicate = [predicate predicateWithSubstitutionVariables:
[NSDictionary dictionaryWithObject:[NSNull null] forKey: #"minor"]];
I found solution
Because of ANY and "=nil" can't be in the same condition (explanation is here Core Data, NSPredicate, ANY key.path == nil)? solution of this problem looks like:
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"SUBQUERY(region.beacons, $x, $x.minor == nil)"];

Filtering values with NSNumber == nil in NSPredicate

I'm trying select values by property with type NSNumber, if property==intValue or property==nil.
But this code ignores values with property==nil.
What the point?
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"(ANY region.beacons.major == %d OR ANY region.beacons.major = nil)
AND (ANY region.beacons.minor == %d OR ANY region.beacons.minor = nil)",
rangedBeacon.major.intValue, rangedBeacon.minor.intValue];
UPD: The most simple doesn't work too:
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"ANY region.beacons.minor = nil"];
UPD 2: Probably problem because of ANY+nil
According to this answer Core Data, NSPredicate, ANY key.path == nil ANY select only NOTNULL values.
Simply test for one of the properties of minor; for example:
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"region.beacons.minor.length = 0"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"region.beacons.minor.stringValue = nil"];
EDIT I recognized, that you should remove the ANY
If minor is nil, any call would return in nil as well, or 0 for numeric types.
For one thing, I notice you're using the assignment operator = instead of equality == while comparing with nil.
Also, try comparing with the Singleton NSNull instance [NSNull null].

NSPredicate - Dynamic predicate with arguments

I am new to NSPredicates, but I know the basics.
This is how my predicate looks now:
[NSPredicate predicateWithFormat:#"name contains[c] %#", searchText];
What I want to do, is to be able to create a predicate like this (Pseudo code):
NSPredicate *myPredicate = [NSPredicate predicateWithBaseString:#"name contains[c] _ARGUMENT_"];
And then use it in, for example a loop (pseudo code):
for(NSString *searchString in self.allStrings)
{
NSPredicate *myNewPredicate = [myPredicate predicateWithArgument:searchString];
//Here I do the searchOperations with my new predicate
}
I want to do this so that I can have a few base predicates and then put parameters on them for searching. I do not want to create my predicate again.
Hopefully you understand.
Thank you!
Use following
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:#"name contains[c] $ARGUMENT"];
for(NSString *searchString in self.allStrings)
{
NSPredicate *myNewPredicate = [myPredicate predicateWithSubstitutionVariables:#{#"ARGUMENT" :searchString}];
//Here I do the searchOperations with my new predicate
}
also I suggest to read this article http://nshipster.com/nspredicate/
It seems as if i just found the answer. I can use predicateWithSubstitutionVariables like this:
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"name contains[c] $searchText"];
NSDictionary *sub = [NSDictionary dictionaryWithObject:searchText forKey:#"searchText"];
NSPredicate *filter = [resultPredicate predicateWithSubstitutionVariables:sub];

Resources