TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: How to filter @Query with specific Value?

Forums > SwiftUI

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!

2      

Hi,

I did a sample of your project, I had to add the @Model macro to the Docket class because the Query requires that 'Docket' conform to 'PersistentModel', I added the uniqueId property to the Docket init and filtered the query by docket.uniqueId == patientId

ContentView

struct ContentView: View {
    @Environment(\.modelContext) var modelContext
    @Query(sort: \Patient.lastName) var patients: [Patient]

    var body: some View {
        NavigationStack {
            List(patients) { patient in
                NavigationLink {
                    DocketDetailView(patient: patient)
                } label: {
                    Text(patient.lastName)
                }

            }
            .toolbar(content: {
                ToolbarItem(placement: .topBarTrailing) {
                    Button("Add Sample") {
                        try? modelContext.delete(model: Patient.self)

                        for i in 0..<10 {
                            let id = UUID()
                            let sample = Patient(lastName: "Patient \(i)", uniqueId: id)

                            for j in 0..<5 {
                                let sampleDocket = Docket(name: "Patient \(i) - Docket \(j)", orderIndex: j, uniqueID: id, referredId: id)

                                modelContext.insert(sampleDocket)

                            }
                            modelContext.insert(sample)
                        }
                    }
                }
            })
        }
    }
}

DocketDetailView

struct DocketDetailView: View {
    var patient: Patient
    @Query(sort: \Docket.orderIndex) var dockets: [Docket]

    init(patient: Patient) {
        self.patient = patient

        let patientId = patient.uniqueId
        let predicate = #Predicate<Docket> { docket in
            docket.uniqueId == patientId
        }
        _dockets = Query(filter: predicate, sort: \Docket.orderIndex)

    }
    var body: some View {
        List(dockets) { docket in
            Text(docket.name ?? "")
        }
    }
}

3      

@Hectorcrdna, thank you SOOO much!!!

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.