This is the error I got,
Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.
Here is the code,
class AdsViewModel: ObservableObject{
@Published var ads: [AdPost]?
@Published var alertMessage = ""
@Published var showAlert = false
@Published var createAd = false
@Published var isWriting = false
func fetchAdPosts() async {
do{
let db = Firestore.firestore().collection("Ads")
let ads = try await db.getDocuments()
DispatchQueue.main.async {
self.ads = ads.documents.compactMap({ ad in
return try? ad.data(as: AdPost.self)
})
}
}
catch{
self.alertMessage = error.localizedDescription
showAlert.toggle()
}
}
}