I tried my first steps into SwiftData and now i'm stuck for the past 4 weeks. I hope, somebody can help me:
I created a tiny, where you can do treatment planning for Patients. I created a model using SwiftData and iCloudkit:
import Foundation
import SwiftData
@Model
final class Patient {
var lastName: String = ""
let uniqueId: UUID
@Relationship(deleteRule: .cascade) var dockets: [Docket]? = [Docket]()
init(lastName: String = "", uniqueId: UUID = UUID()) {
self.lastName = lastName
self.uniqueId = uniqueId
}
}
class Docket {
var name: String?
var orderIndex: Int?
let uniqueId: UUID?
var referredId: UUID?
// because of iCloudkit:
var patients: [Patient]? = [Patient]()
init(name: String? = "", orderIndex: Int?, referredId: UUID?) {
self.name = name
self.orderIndex = orderIndex
self.referredId = referredId
}
}
Now have a view with a list of patients, and each item leading to an Patient Detail View. In this view, i only want to show all related dockets, sorted by orderIndex. Because i want to use .onMove to reorder the Dockets and dynamically update the orderIndex-value, i need @Query to fetch all the related dockets (for this, i use the following solution )
struct DocketDetailView: View {
@Bindable var patient: Patient
@Query (sort: \Docket.orderIndex) var dockets: [Docket]
var body: some View {
// Show all related dockets
ForEach(dockets) { docket in
Text(docket.name)
}
}
i tried to everything to filter the Query directly, or to copy the Patient.uniqueId to the Docket.referredId and to something like
@Query var dockets: [Docket]
init() {
let predicate = #Predicate<Docket> { docket in
docket.referredId == patient.uniqueId
}
_dockets = Query(filter: predicate, sort: \Docket.orderIndex)
}
but it didn' work at all.
I hope, somebody can help me! After 4 weeks trying to solve this error on my own, i'm kinda desperate.
Thank you!