Swiftdata - unsupportedPredicate for enum properties - XCode 15 b7

Hello,

I'm trying to use a #Predicate filtering by an Enum property. But in runtime I getSwiftData.SwiftDataError._Error.unsupportedPredicate

Somehow I couldn't find any recent posts related to this issue. Has anyone encountered this or been able to solve/workaround it? Will it be ever fixed?

What is the point in supporting stored enums if I cannot use them to run a query? I'm struggling with this since beta 1, it really seems that SwiftData is not a priority for Apple right now. I'm really trying to stick with it but I'm running into issues that are indefinitely delaying all my development every day.

@Model final class ModelA {
    var name: String
    enum ModelAType: Int, Codable { case type1 = 1, type2 }
    var type: ModelAType
    // ...
}
// ...
let predicateConstant = ModelA.ModelAType.type1
#Predicate<ModelA> { $0.type == predicateConstant }

I had a similar problem with Beta 6, and still having it after upgrading to Beta 8. It seems that using a Boolean test against a stored enum value always fails within a #Predicate definition. I've tried to make it work both with an @Query macro and a 'let myPredicate = #Predicate<MyClass> { $0.someValue == myStoredEnum.someValue && ...}' statement. One other strange thing is that when I inspect one particular enum definition, the raw value shows as being of type String, when the enum is clearly designated 'enum: Int, Codable {...}

I did a little more digging and came across this post "SwiftData. Predicate. Issue accessing self (Entity) inside a Predicate". This gave me the insight into a solution that worked for me. My use case is that I'm using a Picker to select from a stored enum value, and then using that value to build a search predicate. Since stored enum values have an implicit 'self' prepended to their value references (ie. myStoredEnum.value1 could be fully written out as self.myStoredEnum.value1), I was unknowingly throwing sand in the search predicate gears with the implicit 'self'. My workaround is to use an intermediate variable to hold the enum value and then build the search predicate using the intermediate variable instead of directly with the enum value. Yes it's cludgy, but it does seem to work. Hopefully Apple will figure it out so that references to 'self' don't fail in search predicates and we can delete the workaround.

I did try with RC 1 with no luck. I filled a Feedback issue in Beta 8 that is currently under investigation but still don't know if it is a bug or the intended behaviour.

@OldBrian If I understood you correctly, you tried something like:

struct testView: View {
  @State private var predicate: Predicate<ModelA> = .true
  private var filterType: ModelA.ModelAType = .type1

  var body: some View {
      EmptyView()
          .onAppear {
              predicate = #Predicate<ModelA> { $0.type == self.filterType }
          }
   }
}

But it doesn't seem to work either as @sroebert said. Could you share an example?

Swiftdata - unsupportedPredicate for enum properties - XCode 15 b7
 
 
Q