ios – Compiler is unable to type-check Swift Data expression in reasonable time to render in Swift UI view


Using Swift UI, in XCode 15.2, when converting a variable into a Swift Data @Query attribute with a simple predicate, my application no longer compiles and throws the error:

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression...

My goal in the query is to get all records with a given string in a ‘drinkType’ field matches beer. I need this to get the count of all records that match this value in that column. So I can present it in the view

When a user selects a button, it should add a new record to this model in swift data, recalculate the count, and update the UI.

the contents of the contentView are as such:

import SwiftUI
import SwiftData

struct ContentView: View {
//the environment
@Environment(\.modelContext) var modelContext

//the predicate
@Query(filter: #Predicate<DrinkModel> { 
    drinkModel in
    drinkModel.drinkType == "beer"
}) var beers: [DrinkModel]

//convert these variables into the format above once type checking sorted out
@State var wine:Int = 0
@State var shots:Int = 0
@State var cocktails:Int = 0
@State var drinks:Int = 0

var body: some View {
    ZStack {
        Color.teal.ignoresSafeArea()
        
        VStack {
            Text("DrinkLess v0.01").padding()
            Spacer()
            HStack {
                VStack {
                    Button("🍺", action: { self.recordDrink(drinkType: .Beer)}).buttonStyle(LiquorButtonStyle())

                    Text("\(beers.count) Beers")
                }
                VStack {
                    Button("🍷", action: {self.recordDrink(drinkType: .Wine)}).buttonStyle(LiquorButtonStyle())
                    Text("\(wine) Wines")
                }
            }
            HStack {
                VStack {
                    Button("🥃", action: {self.recordDrink(drinkType: .Shot)}).buttonStyle(LiquorButtonStyle())
                    Text("\(shots) Shots")
                }
                VStack {
                    Button("🍸", action: {self.recordDrink(drinkType: .Cocktail)}).buttonStyle(LiquorButtonStyle())
                    Text("\(cocktails) Cocktails")
                    
                }
            }
            Spacer()
            Text("Total Drinks this week").padding()
            Text(String(beers+wine+shots+cocktails)).font(.bold(.system(size: 38))())
        }
        .padding()
    }
}

private func recordDrink(drinkType: drinkType) {
    let dm = DrinkModel(drinkType: drinkType.rawValue, date: Date())
    modelContext.insert(dm)
}
}

Returning the variable to be like the others removes this error, so it seems likely the error comes from the processing of the predicate and/or the query.

if it helps, here is the model being queried. Its very simple:

import SwiftData
import Foundation

@Model

class DrinkModel {
var drinkType: String
var date: Date

init(drinkType: String, date: Date) {
    self.drinkType = drinkType
    self.date = date
}

}

And an enum I use to map a limited number of strings to types

//wprkaround since Swift Data has bug not supporting Enum values
enum drinkType: String {
case Beer = "beer"
case Wine = "wine"
case Shot = "shot"
case Cocktail = "cocktail"
}

I need to be able to observe the @Query variables for changes, and update Swift UI when the values do.

What can I do if anything to simplify either the query or abstract the UI to make this feasible?

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img