Correct way to create a NSPredicate - iphone

i found 2 different ways to create a NSPredicate.
Way 1:
NSExpression *exprName = [NSExpression expressionForKeyPath:#"name"];
NSExpression *exprFilter = [NSExpression expressionForConstantValue: name ];
NSPredicate *predicate = [NSComparisonPredicate predicateWithLeftExpression: exprName
rightExpression: exprFilter
modifier: NSDirectPredicateModifier
type: NSContainsPredicateOperatorType
options: NSCaseInsensitivePredicateOption];
Way 2:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"name CONTAINS[c] %#", name];
What is the best way to create a NSPredicate and also prevent SQLInjection?

The first way is useful if you are setting up a complex predicate programmatically. Other than that, the second way is fine. You don't have to worry about SQL injection with Core Data.
relevant
also relevant

Related

NSPredicate not filtering correctly

I'm trying to setup a NSPredicate to filter data.
I have a property of an entity called code that is in format like 55.534.
I'm trying to fetch all data in a range, for example 50-60.
I have this but it doesn't work.
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:#"ANY code BETWEEN %#", [NSArray arrayWithObjects:self.predicateFilterStart, self.predicateFilterEnd, nil]];
First of all, your SQL syntax is wrong. Your predicate format should be
ANY code BETWEEN x AND y
Secondly, %# formatter should receive NSString, while you're passing NSArray.
Try this:
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:#"ANY code BETWEEN %# AND %#", self.predicateFilterStart, self.predicateFilterEnd];

problem with a compound nspredicate

what is a problem with this predicate:
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:#"(category like[cs] %#) AND (aroundme==YES)",category];
?
Thks.
There is no such thing as an [s] comparison modifier. The only ones are [c], [d], or [cd].
I think you need to instead pass in [NSNumber numberWithBool:YES] to aroundme==%# like so:
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:#"(category like[cd] %#) AND (aroundme==%#)",category, [NSNumber numberWithBool:YES]];
That is unless you are looking for the string 'YES'. Then, the resulting predicate should quote YES.
excuse me the problem was not there .. i did'nt add the category attribute to the DB

Setting predicate to search in Core Data

I need to perform search Core Data entities in my app for iPhone.
In my Core Data I have an entity 'myEntity', that has two properties 'stringOne' and 'stringTwo'; both defined as NSString.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"stringOne contains[cd] %#", self.keyword4Search];
[fetchRequest setPredicate:predicate];
My code above only search entities that contains keyword4Search in stringOne only.
What I need is to search entities that contains keyword4Search in [stringOne OR stringTwo].
I expect kind of
predicateWithFormat:#"stringOne or stringTwo contains[cd] %#", self.keyword4Search];
where should I look?
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"stringOne contains[cd] %# OR stringTwo contains[cd] %#", self.keyword4Search, self.keyword4Search];

Core Data NSPredicate checking for BOOL value

I am currently having an issue pulling all data from db whereby i.e 1 parameter is TRUE.
I am using NSPredicate and below is a sample code
NSManagedObjectContext *context = managedObjectContext_;
if (!context) {
// Handle the error.
NSLog(#"ERROR CONTEXT IS NIL");
}
NSEntityDescription *entity = [NSEntityDescription entityForName:#"tblcontent" inManagedObjectContext:managedObjectContext_];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"bookmarked == YES"];
[request setPredicate:predicate];
I tried setting predicatewithformat to almost everything but it still does not pull out bookmarks which have a YES value.
I even tried (#"bookmarked == %d",YES) but with not luck. I don't want to have to get the whole array and then filter it manually by doing if(object.bookmarked == YES) .....blabla.
I will really appreciate some help.
Many thanks.
Based on Apple Document Here, we can use the following two methods to compare Boolean:
NSPredicate *newPredicate = [NSPredicate predicateWithFormat:#"anAttribute == %#",[NSNumber numberWithBool:aBool]];
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:#"anAttribute == YES"];
However, the above predicate cannot get out the ones with empty anAttribute. To deal with an empty attribute, you need the following method according to Apple document here:
predicate = [NSPredicate predicateWithFormat:#"firstName = nil"]; // it's in the document
or
predicate = [NSPredicate predicateWithFormat:#"firstName == nil"]; // == and = are interchangeable here
Sneaking in with the Swift 3/4 answer:
let predicate = NSPredicate(format: "boolAttribute == %#", NSNumber(value: true))
We have to use NSNumber apparently because a literal bool is not acceptable per Apple.
Stolen from here ;)
For some reason, Flow's solution would not work for me:
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:#"anAttribute == YES"];
However, this did:
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:#"anAttribute == 1"];
I'm late to the party and as discussed using 0 and 1's is the way to go however there is a better way to show this by using NSNumber BOOL literals like #YES or #NO. It converts it to a 1 or a 0 but is more visually friendly.
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:#"anAttribute == %#", #NO];
Core Data entities does not have any default values when you create attributes for Entity, so to make your predicate work you should either set default values for boolean attributes or use predicate in this way.
If you provide default value (NO or YES) for any boolean property of Entity then use predicate like this
[NSPredicate predicateWithFormat:#"boolAttribute == %#", #NO];
[NSPredicate predicateWithFormat:#"boolAttribute == NO", #NO];
[NSPredicate predicateWithFormat:#"boolAttribute == %0"];
If you do not have default values or some of Entities was already created without default values then to filter by false value use this sentence:
[NSPredicate predicateWithFormat:#"boolAttribute == %# || boolAttribute == nil", #NO];
Swift:
fetchRequest.predicate = NSPredicate(format: "%K == NO",#keyPath(MyEntity.isOccupied))
or
fetchRequest.predicate = NSPredicate(format: "%K == NO",\MyEntity.isOccupied)
You haven't mentioned what results you're getting. Two things that are missing from your code listing is where you set your request's entity and where you actually ask the context to execute the fetch request. I'd start there.
For me, it happened coz of assign value in Object is wrong when saving.
You have to save like this
YourNSMNanagedObject.visibleBoolean = [[NSNumber alloc] initWithBool:false]
Then all predicate will work when fetching.
E.g.
// Filter only unvisible item
NSPredicate *favouriteFilter = [NSPredicate predicateWithFormat:#"visibleBoolean==NO"];
I keep getting bit by this one and it's not always clear that booleans in core data get saved as NSNumbers.
For the most part I make sure when creating entities I set #NO for any Boolean properties but there's always the case where I spend a whole bunch of time trying to figure out why a test doesn't pass when I've forgot to set #NO on entity creation.
This might not be the best choice for clear code but I've now started to always use != predicates for booleans in core data like (isCompleted != YES) instead of (isCompleted == NO). The fact nil == false pattern is true everywhere else but not in core data predicates and properties can be very confusing.

Use NSPredicate to filter by object attribute

I have a mutable array of custom objects. I want to filter that array by attribute of the object, for example myObject.attributeOne.
How can I create the NSPredicate to use with
[myArrayOfObjects filterUsingPredicate:<the_predicate>]
Use it in this way:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"FriendStatus == 1"];
NSMutableArray *filtered = [MessageArray filteredArrayUsingPredicate:predicate];