Swift- Waiting for asynchronous for-in loop to complete before calling completion handler swift

I'm baffled as to what is the best way to achieve this. I am trying to keep a running total of Double values that I am looping through and adding together, via a network call. Everything I've read says to use DispatchGroup. My completion either calls too early or doesn't get called at all and I've tried every configuration of .enter, .leave, and .wait that I can think of.

    let group = DispatchGroup()
    var runningTotal: Double = 0.00
ref.observeSingleEvent(of: .value) { (snapshot) in
    guard let bills = snapshot.value as? [String: AnyObject] else {
        //error
        return
    }

    for billId in bills.keys {
        group.enter()
        print("Entering")
        Database.database().reference().child("bills").child(billId).observeSingleEvent(of: .value, with: { (snapshot) in
            guard let bill = snapshot.value as? [String: AnyObject] else {
                return
            }
            if let amount = bill["amount"] as? Double {
                runningTotal += amount
            }
            group.leave()
            print("Leaving")
        })
    }
    completion(runningTotal)
}
group.wait()

}


#swift

5 Likes150.20 GEEK