I am working with SwiftData and SwiftUI and it isn’t clear to me whether it is good apply to have a @ModelActor instantly populate a SwiftUI view. For instance when having to mix handbook lab outcomes and clinial outcomes from HealthKit. The Scientific lab outcomes are an async operation:
@ModelActor
actor LabResultsManager {
func fetchLabResultsWithHealthKit() async throws -> [LabResultDto] {
let manualEntries = attempt modelContext.fetch(FetchDescriptor())
let clinicalLabs = (attempt? await HealthKitService.getLabResults()) ?? []
return (manualEntries + clinicalLabs).sorted {
$0.date > $1.date
}.map { LabResultDto(from: $0) }
}
}
struct ContentView: View {
@State non-public var labResults: [LabResultDto] = []
var physique: some View {
Checklist(labResults, id: .id) { lead to
VStack(alignment: .main) {
Textual content(end result.testName)
Textual content(end result.date, model: .date)
}
}
.activity {
do {
let labManager = LabResultsManager()
labResults = attempt await labManager.fetchLabResultsWithHealthKit()
} catch {
// Deal with error
}
}
}
}