This SwiftData issue has me stumped. My app, when uploaded via cable works fine but when it is deployed via TestFlight it crashes immediately on startup while trying to do the first fetch for data.
Here is a screenshot of the crash report ...
I've tried the following things to fix the issue:
- Disabled
Swift Compiler - Code Generation
configuration toNo Optimization
for both Debug and Release - I tried calling the
fetch
from atask()
modifier instead ofonAppear()
- I deleted the app from the device before reinstalling it via TestFlight
- I tried it on a different device with a different iCloud account.
Here is the code that is calling the fetch()
inside a ViewModel.
func refresh(_ ctx: ModelContext) {
records.removeAll()
let wRecs: [WeightRecord] = fetchAll(ctx)
records.append(contentsOf: wRecs)
let bpRecs: [BloodPressureRecord] = fetchAll(ctx)
records.append(contentsOf: bpRecs)
let mdRecs: [MedicationTakenRecord] = fetchAll(ctx)
records.append(contentsOf: mdRecs)
let bmRecs: [BowelMovementRecord] = fetchAll(ctx)
records.append(contentsOf: bmRecs)
let seRecs: [SideEffectRecord] = fetchAll(ctx)
records.append(contentsOf: seRecs)
let fdRecs: [FoodConsumedRecord] = fetchAll(ctx)
records.append(contentsOf: fdRecs)
let pmpRecs: [PumpChangeRecord] = fetchAll(ctx)
records.append(contentsOf: pmpRecs)
records.sort { lRec, rRec in
lRec.recordedOn < rRec.recordedOn
}
}
func fetchAll<R: PersistentModel & Record>(_ ctx: ModelContext) -> [R] {
let fetchDescriptor = FetchDescriptor<R>(
sortBy: [SortDescriptor(\.recordedOn)]
)
return (try? ctx.fetch(fetchDescriptor)) ?? []
}
Any ideas what might I am doing wrong?