I have been dealing with an error for almost 2 days now that caused my programme to crash on runtime with Thread 10: EXC_BAD_ACCESS (code=1, address=0x0)
error , only when using Release mode.
After many trial and errors and narrowing down the root problem I became suspicious to #Predicate and Generics being the root cause of the problem so I made these views to test it out.
import SwiftUI
import SwiftData
struct DataBaseTestGeneric<Model : PersistentModel>: View {
@State private var models: [Model] = []
var body: some View {
viewLoader{
let reporter = Reporter()
let pred = #Predicate<Model>{ model in
return true
}
models = await reporter.fetch(pred)
}content: {
List{
ForEach(models){ model in
Text("\(model.id)")
}
}
}
}
}
and a non-Generic version :
import SwiftData
struct DatabaseTest: View {
@State private var transactions: [Transaction] = []
var body: some View {
viewLoader {
let reporter = Reporter()
let pred = #Predicate<Transaction>{ dec in
return true
}
let decs = await reporter.fetch(pred)
transactions = decs
}content:{
List{
ForEach(transactions){transaction in
Text("\(transaction.id)")
}
}
}
}
}
to give you an insight viewLoader
implentations is :
struct viewLoader<Content : View>: View {
var state : LoadingView.States = .loading
let loadingTask : () async -> Void
@State private var isLoading = true
@ViewBuilder var content : Content
var body: some View {
if isLoading{
LoadingView(state)
.task {
await Task.detached(priority:.high){
await loadingTask()
}.value
isLoading = false
}
}else{
content
}
}
}
and I am accessing SwiftData using a background thread ( by implementing @ModelActor
. the problem is that the code always crash on runtime when I am trying to fetch the data using reporter.fetch
function.
To make things even more weird I have to add the fact that Reporter
class also have another function called fetchAll
as follow :
func fetchAll<T>(_ model : T.Type) async -> [T] where T : PersistentModel {
let desc = FetchDescriptor<T>()
let result = try? await context.fetch(desc)
guard let result else {
assertionFailure("Error fetching \(model) from context")
return []
}
return result
}
if i replace this function with reporter.fetch
(which takes a predicate) the code will not crash either with or without using Generics , which brings me to the point that #Predicate
is causing mayhem somehow !
PS: I am using Xcode Version 16.1 (16B40) , on macOs Sequoia Version 15.2 Beta (24C5089c)