zevij
zevij

Reputation: 2446

CoreData, NSPredicate and fetchRequest puzzling behaviour - or is it just me?

I have encountered an odd scenario when using CoreData, NSPredicate and fetchRequest.

My predicate is:

predicate = NSPredicate(format: "(status == %d) AND (watched == NO) AND (user_id == %@)", argumentArray: [Int(ItemStatus.Shortlisted.rawValue), false, userId])

If I run fetchRequest with the above predicate, I get 0 results. If I swap the user_id clause with watched clause, I get the expected result.

If I use NSCompundPredicate, with the individual clauses broken down to sub-predicates, using:

request.predicate = NSCompoundPredicate(type: .AndPredicateType, subpredicates: [subPredicate1, subPredicate2, subPredicate3])

The outcome is what I expect.

I am not clear why why is there a discrepancy between the three approaches.

Thoughts?

Upvotes: 0

Views: 57

Answers (1)

Walt Sellers
Walt Sellers

Reputation: 3939

Taking a guess, (My Core Data is a little rusty) it looks like you have specified 3 arguments in the argument array, but the predicate is only using 2 of them.

With (watched == NO), you've specified the value inline rather than with a "%" delimited format, so it won't use the "false" from the argument array.

Upvotes: 2

Related Questions