I am working on a SwiftData app, and when I query any of the ones with the Predicate<Time> (NOT Predicate<Session>) I get Query encountered an error: SwiftData.SwiftDataError(_error: SwiftData.SwiftDataError._Error.unsupportedPredicate). The code for my file is below.
import SwiftData
import SwiftUI
struct timeListView: View {
@Environment(\.modelContext) var modelContext
@Query(sort: \Time.date, order: .reverse) var times: [Time]
@Query(filter: #Predicate<Time> { time in
time.unfortime! < 6000
}, sort: \Time.date, order: .reverse) var timesSub60: [Time]
@Query(filter: #Predicate<Time> { time in
time.unfortime! < 3000
}, sort: \Time.date, order: .reverse) var timesSub30: [Time]
@Query(filter: #Predicate<Time> { time in
time.unfortime! < 2000
}, sort: \Time.date, order: .reverse) var timesSub20: [Time]
@Query(filter: #Predicate<Time> { time in
time.unfortime! < 1000
}, sort: \Time.date, order: .reverse) var timesSub10: [Time]
@Query(filter: #Predicate<Time> { time in
time.unfortime! < 500
}, sort: \.date, order: .reverse) var timesSub5: [Time]
@Query(sort: \Time.date, order: .forward) var timesOldest: [Time]
@Query(sort: \Time.unfortime, order: .forward) var timesBest: [Time]
@Query(filter: #Predicate<Session> { session in
session.selected == true
}) var selectedSession: [Session]
@State var forTime: String = ""
@State var formattedTimes: [String] = []
@State var sort: [Time] = []
@Binding var showDate: Bool
private var columns: [GridItem] {
return [
.init(.adaptive(minimum: 85, maximum: 85))
]
}
// format date and display
var body: some View {
NavigationStack {
VStack {
HStack {
if showDate {
showDateList()
} else {
noDateList()
}
}
Spacer()
}
.toolbar {
ToolbarItem {
Menu {
Section("Sort") {
Button("Newest") {
sort = times
}
Button("Best") {
sort = timesBest
}
Button("Oldest") {
sort = timesOldest
}
Button("Sub 60") {
sort = timesSub60
}
Button("Sub 30") {
sort = timesSub30
}
Button("Sub 20") {
sort = timesSub20
}
Button("Sub 10") {
sort = timesSub10
}
Button("Sub 5") {
sort = timesSub5
}
}
} label: {
Label("Menu", systemImage: "line.3.horizontal.decrease.circle")
}
.menuOrder(.fixed)
}
}
.navigationTitle("Times")
}
}
private func getDate(time: Time) {
let date = time.date!
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minute = calendar.component(.minute, from: date)
let second = calendar.component(.second, from: date)
let timeToFormatComponents = DateComponents(calendar: calendar,
year: calendar.component(.year, from: date),
month: calendar.component(.month, from: date),
day: calendar.component(.day, from: date),
hour: hour,
minute: minute,
second: second)
let timeToFormat = timeToFormatComponents.date!
let formatter = DateFormatter()
formatter.dateFormat = "M/dd/yy', 'h:mm a"
forTime = formatter.string(from: timeToFormat)
}
@ViewBuilder func showDateList() -> some View {
List {
ForEach(sort) { time in
if time.session == selectedSession[0] {
NavigationLink(destination: timeDetailView(time: time)) {
HStack {
Text(time.time!)
.onAppear {
getDate(time: time)
time.formatted = forTime
}
Spacer()
Text(time.formatted!)
}.swipeActions {
Button("Delete", role: .destructive) {
modelContext.delete(time)
}
}
}
.buttonStyle(.plain)
}
}
}
.onAppear {
sort = times
}
}
@ViewBuilder func noDateList() -> some View {
LazyVGrid(columns: columns, spacing: 0) {
ForEach(sort) { time in
if time.session == selectedSession[0] {
NavigationLink(destination: timeDetailView(time: time)) {
TimeBoxView(time.time ?? "Not Found")
.frame(minWidth: 100)
.padding()
.onAppear {
getDate(time: time)
time.formatted = forTime
}
}
.buttonStyle(.plain)
}
}
}
.onAppear {
sort = times
}
}
}
#Preview {
timeListView(showDate: .constant(true))
.modelContainer(
for: [Time.self])
}
Any help appreciated.