ebbandflows
ebbandflows

Reputation: 73

Filter to NSFetchRequest

hi i'm not really understanding how the fetch filter works can anyone help me please? So i currently have this as my fetchall function which displays all of my items within my entity

im having trouble of filtering only one attribute which is a boolean. I want it to only display attributes that are true.

thankyou!

 func fetchAllItems(){
        let request = NSFetchRequest<NSFetchRequestResult>(entityName: "AllItems")

        do{
            let result = try managedObjectContext.fetch(request)
            beastList = result as! [AllItems]
        } catch { print("\(error)")
        }
    }

Upvotes: 0

Views: 1107

Answers (1)

user1046037
user1046037

Reputation: 17725

Code:

let request = NSFetchRequest<NSFetchRequestResult>(entityName: "AllItems")

request.predicate = NSPredicate(format: "something = %@", argumentArray: [true])

Note: Replace something with your boolean field name

Better way to create request

let request : NSFetchRequest<AllItems> = AllItems.fetchRequest()

Naming convention:

It is better to name your entity in singular form, so that each row in your entity could be in singular form.

AllItems seems very generic, if you are storing cars, the entity name could be Car. The variable that stores the result of the fetch request could be cars.

Reference:

Upvotes: 2

Related Questions