42

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.

James Zaghini
  • 3,895
  • 4
  • 45
  • 61
user281300
  • 1,427
  • 2
  • 19
  • 31

9 Answers9

70

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
Mingming
  • 2,189
  • 20
  • 21
29

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

davidrynn
  • 2,246
  • 22
  • 22
  • OMG! You saved my life... WHY is it working with `false` but not with `true` ... It took me a while to even find the line that makes my app crash, because the stack trace was not helpful at all. – randomcontrol May 20 '21 at 09:04
13

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"];
Dan Loughney
  • 4,647
  • 3
  • 25
  • 40
10

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];
Patrick
  • 2,035
  • 17
  • 27
5

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];
Nikolay Shubenkov
  • 3,133
  • 1
  • 29
  • 31
  • The null check is important, especially as once the fetch is done it can appear that bool entries are false, when in fact they are null – lewis Jun 09 '20 at 10:07
2

Swift:

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

or

fetchRequest.predicate = NSPredicate(format: "%K == NO",\MyEntity.isOccupied)
Hemang
  • 26,840
  • 19
  • 119
  • 186
1

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"];
Thein
  • 3,940
  • 2
  • 30
  • 34
1

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.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • Hi I haven't included that in the code. The results am getting is basically the whole lot. It is as if no filtering is working. – user281300 Sep 08 '10 at 16:57
  • Hi again I think there is something wrong before that. I.e every single content coming back has object.bookmarked = YES even though I have set it to NO by default. I will need to check it out. I reckon , the predicate is working. Thanks anyways. – user281300 Sep 08 '10 at 16:59
  • 2
    Hah it is working fine now. stupid me forgot to reset the db from the simulator first doh!! Anyways the code above is fine. – user281300 Sep 08 '10 at 17:04
0

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.

SpaceTrucker
  • 1,168
  • 7
  • 16