Search code examples
iosswiftcore-datansfetchrequest

How to use two NSPredicates for fetch request - Swift 2


I was trying to use two NSPredicates for a single fetch request by doing,

let predicate1 = NSPredicate(format: "object1 == nil", "object1")
let predicate2 = NSPredicate(format: "object2 == nil", "object2")
let predicate = NSCompoundPredicate(type: NSCompoundPredicateType.OrPredicateType, subpredicates: [predicate1, predicate2])

request.predicate = predicate

the objects are a subclass of NSManagedObject and I am trying to fetch all items that have not got a relation with either object.

However it does not seem to be working, can someone have any suggestions on where I may be going wrong ?


Solution

  • Try this,

    let predicate1 = NSPredicate(format: "object1 == nil && object2 == nil", "object1","object2")