eli6
eli6

Reputation: 1034

Why can my NSPredicate filter for one entity attribute but not the other?

I want to use an NSPredicate to display school subjects stored in Core Data that have their start date set to a specific month. I currently store the month names as strings. The problem is that the debugger prints out "Number of results: 0" when I try to display subjects filtered by "startMonth" (I try with August in this example, which should return English but doesn't!).

However, and this is the weird part, if I filter by the name "English" I get 1 result (i.e. English. I also print out its "startMonth" in the loop afterwards and the startMonth is indeed "August"). Does anyone know why everything works out fine when I search for names but not for months? Of course I've double checked that the month is correctly capitalized, that I didn't confuse startMonth and endMonth, etc... I just can't figure out why I can filter for one attribute but not for the other...

So, this isn't working, the debugger prints out "Number of results: 0" (should return 1 result):

func getSubjects() {

    let delegate = AppDelegate()
    let monthPredicate = NSPredicate(format: "%K = %@", "startMonth", "August")
    let fetchRequest: NSFetchRequest<Subject> = Subject.fetchRequest()
    fetchRequest.predicate = monthPredicate

    do {
        let searchResults = try delegate.getContext().fetch(fetchRequest)

        print("Num of results: \(searchResults.count)")
        for subject in searchResults as [NSManagedObject] {
            print(subject.value(forKey: "name")!)
            print(subject.value(forKey: "startMonth")!)
            print(subject.value(forKey: "endMonth")!)
        }
    } catch {
        print("Error in request")
    }

}

This is working, however:

func getSubjects() {

    let delegate = AppDelegate()
    //Only this line has changed:
    let monthPredicate = NSPredicate(format: "%K = %@", "name", "English")
    let fetchRequest: NSFetchRequest<Subject> = Subject.fetchRequest()
    fetchRequest.predicate = monthPredicate

    do {
        let searchResults = try delegate.getContext().fetch(fetchRequest)

        print("Num of results: \(searchResults.count)")
        for subject in searchResults as [NSManagedObject] {
            print(subject.value(forKey: "name")!)
            print(subject.value(forKey: "startMonth")!)
            print(subject.value(forKey: "endMonth")!)
        }
    } catch {
        print("Error with request")
    }

}

Now the debugger prints:

"Num of results: 1
English
August
December"

Thank you in advance!

Upvotes: 0

Views: 86

Answers (1)

Nirav D
Nirav D

Reputation: 72410

When you have entered the value with startMonth may be it has also included extra leading or trailing space with it.

You can resolve this issue using two ways.

  1. When you entering value trim the extra space.
  2. Use CONTAINS with predicate instead of = it will check that string is contains given string or not.

Upvotes: 1

Related Questions