简体   繁体   中英

Core Data NSPredicate checking for BOOL value

I am currently having an issue pulling all data from db whereby ie 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

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

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

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

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.

Eg

// Filter only unvisible item
NSPredicate *favouriteFilter = [NSPredicate predicateWithFormat:@"visibleBoolean==NO"];

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.

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.

Swift:

fetchRequest.predicate = NSPredicate(format: "%K == NO",#keyPath(MyEntity.isOccupied))

or

fetchRequest.predicate = NSPredicate(format: "%K == NO",\MyEntity.isOccupied)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM