I am trying to loop through two Firebase database references, firstRef
and secondRef
, observe the respective values with the observeSingleEvent
Firebase async method, and append the values to two arrays, firstArray
and secondArray
. Once this has been completed, I would like to notify the dispatch group and execute a completion handler. However, when the following method is called the two arrays are always unexpectedly found nil. I'm not too sure why this is or whether this problem arises because I have nested asynchronous functions. I know force unwrapping is also syntactically frowned upon, but, in this case, I expect the two arrays to always be populated with data retrieved from the database.
func loop(inputArray: [inputType], doneLoop: @escaping (_ firstArray: [firstType],_ secondArray: [secondType]) -> Void) {
var firstArray: [firstType]?
var nameArray: [secondType]?
let dispatchGroup = DispatchGroup()
for element in inputArray {
let firstRef = Database.database().reference(withPath: "firstPath")
//Enter dispatch group
dispatchGroup.enter()
firstRef.observeSingleEvent(of: .value, with: { snapshot in
//Storing first snapshot value
let firstSnapshotValue = snapshot.value as! firstType
let secondRef = Database.database().reference(withPath: "secondPath")
secondRef.observeSingleEvent(of: .value, with: { snapshot in
//Storing second snapshot value
let secondSnapshotValue = snapshot.value as? String
//Append retrieved values to arrays
firstArray?.append(firstSnapshotValue)
secondArray?.append(secondSnapshotValue)
})
})
//Leave dispatch group
dispatchGroup.leave()
}
//Notify on main queue and execute completion handler
dispatchGroup.notify(queue: .main) {
doneLoop(firstArray!, secondArray!)
}
}
Do I perhaps have to create a new function with an @escaping completion handler? The only problem is that since it is looping through the array, the completion will only be executed once and so only one value would have be populated.
Any help would be much appreciated!