SwiftData within Observable class

Greetings Everyone. I have a question regarding my capstone project. I have an app (which I will introduce later in my Journal) that has more than one Swift Data model. Furthermore, I currently have all the models loaded in one class with observation and distributed in every view that is needed with the @environment macro. Although until now, the app worked smoothly, but I have second thoughts regarding if this is the correct way of using swift Data models in an app.
Thanks

@Yianni1980
Welcome to the community.

Without seeing your code I am not sure what you mean by having all the models in the one class. That said, if it works for you as is then great.

I am sorry, you are right. This is my diabetes management app. This class logs glucose readings, meals, medications. Until now, I used @Query and Model Context in every view needed. Using centralized all in one class and I can say it works as expected, but I don’t know if it is the correct approach. Also, ultimately should I use SwiftData or move to remote (supabase,firebase)?

@Observable
class HealthDataStore {
var glucoseReadings: [GlucoseReading] =
var meals: [Meal] =
var medications: [MedicationLog] =
let context: ModelContext

init(context:ModelContext) {
    self.context = context
}



var latestGlucoseReading: Double {
    let todayReadings =
    glucoseReadings.filter {
        Calendar.current.isDateInToday($0.timestamp) }
    return todayReadings.isEmpty ? 0 : todayReadings.first!.value
}

var sevenDayAverageGlucose: Double {
    averageGlucose(inLastDays: 7)
}

var latestReadingTimeAgo: String {
    guard let latest = glucoseReadings.sorted(by: { $0.timestamp > $1.timestamp }).first else {
        return "No readings"
    }

    let formatter = RelativeDateTimeFormatter()
    formatter.unitsStyle = .full
    return formatter.localizedString(for: latest.timestamp, relativeTo: Date())
}


func fetchAll() {
    do {
        print("fetching from database")
        glucoseReadings = try context.fetch(FetchDescriptor<GlucoseReading>(sortBy: [.init(\GlucoseReading.timestamp, order: .reverse)]))
        meals = try context.fetch(FetchDescriptor<Meal>(sortBy: [.init(\Meal.timestamp, order: .reverse)]))
        medications = try context.fetch(FetchDescriptor<MedicationLog>(sortBy: [.init(\MedicationLog.timestamp, order: .reverse)]))
        print("finishe fetching from database")

    } catch {
        print("Fetch failed:", error)
    }
}

func addGlucose(_ reading: GlucoseReading) {
    context.insert(reading)
    try? context.save()
    fetchAll()
}

func addMeal(_ meal: Meal) {
    context.insert(meal)
    try? context.save()
    fetchAll()
}

func addMedication(_ med: MedicationLog) {
    context.insert(med)
    try? context.save()
    fetchAll()
}

}

OK I understand what you are doing there. The way I have learned MVVM is such that I would have called that a ViewModel which is your Observable class. I would assume that each of GlucoseReading, Meal and MedicationLog are your individual models defined as structs and declared either in individual files or collectively in a single file with each one having @Model preceding the struct definition and some @Relationship to link them together where necessary.

What you are doing with that Observable class is much the same as I would have done it.

Each Model (glucose reading,meal,medicationLog) are classes with @Model declared. and they don’t have a relationship with each other. Assuming that a user can log everything he wants without the need to be linked the models together. If you want i can put some code regarding the declaration of the models for each measurement individually without any relationship with each other. What do you think?

No that’s not necessary to post your model declarations code. If there is no relationship with any of the models then that makes it easier for you.