Skip to content

Commit

Permalink
IMPROVE: updated fetchNewestAlert function to be async (#370)
Browse files Browse the repository at this point in the history
* IMPROVE: updated fetchNewestAlert function to be async

* PR suggestions

---------

Co-authored-by: Mikaela Caron <mikaelacaron@gmail.com>
  • Loading branch information
MilanLabuss and mikaelacaron authored Nov 6, 2024
1 parent edd2cff commit b8055a9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MainTabViewModel {
@MainActor var alert: AlertItem?

/// Update the UI once a new alert is sent
func fetchNewestAlert(ignoring acknowledgedAlerts: [String]) {
func fetchNewestAlert(ignoring acknowledgedAlerts: [String]) async {

var query = Firestore
.firestore()
Expand All @@ -27,14 +27,10 @@ class MainTabViewModel {
.whereField(FirestoreField.id, notIn: acknowledgedAlerts)
}

query.getDocuments { [weak self] snapshot, error in
guard let self,
error == nil,
let documents = snapshot?.documents else {
return
}
do {
let snapshot = try await query.getDocuments()

let newAlert = documents
let newAlert = snapshot.documents
.compactMap {
do {
return try $0.data(as: AlertItem.self)
Expand All @@ -50,6 +46,9 @@ class MainTabViewModel {
self.alert = newAlert
}
}

} catch {
print("Error getting documents: \(error)")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ struct MainTabView: View {
}
}
.onAppear {
viewModel.fetchNewestAlert(ignoring: acknowledgedAlerts.map(\.id))
Task { @MainActor in
await viewModel.fetchNewestAlert(ignoring: acknowledgedAlerts.map(\.id))
}
}
.onChange(of: viewModel.alert) { _, newValue in
guard let id = newValue?.id else { return }
Expand Down

0 comments on commit b8055a9

Please sign in to comment.