bob_mosh
bob_mosh

Reputation: 335

How can I use NSPredicate with a computed property in Swift?

I am trying to create a @FetchRequest in SwiftUI using an NSPredicate, like so:

@FetchRequest(entity: CoreDataRecipe.entity(), sortDescriptors: [], predicate: NSPredicate(format: "isRunning == true")) var runningRecipes: FetchedResults<CoreDataRecipe>

Unfortunately, my isRunning property is a computed property like so:

var isRunning: Bool {
    get {
        activeStep != nil
    }
}

The problem is that NSPredicate will crash since it can't find a string for the computed property when resolving the predicate. Is there any way to make this possible? I would love to use this, since it would allow me to create a nice UX.

Note: I can't test for activeStep as an alternative, because it is a computed property as well with more complex logic.

Upvotes: 2

Views: 683

Answers (0)

Related Questions