I am trying to write a simple database query to fetch some data from my realtime database, I wanted to use Async/Await but I don’t know if it’s possible with observeSingleEvent
Is it possible to do something like this?
func getPromoCodes() async throws -> [PromoCode] {
promoCodes.observeSingleEvent(of: .value) { snap in
guard let snap = snap.value as? [String: Any] else {
return []
}
do {
let jsonData = try JSONSerialization.data(withJSONObject: snap)
let decoded = try JSONDecoder().decode([PromoCode].self, from: jsonData)
return decoded
} catch {
return []
}
}
}
2
Answers
Turns out it's pretty simple, each DatabaseReference has an async
getData
function