The following function is suppose to fetch a list of blocked-users then fetch blocked-by-users merge the two lists and return it as a [String]
.
The output to the console is the following which tells me that the return
blockedList
is being called early - how can I force for the two observeSingleEvents functions are finished before passing the array back to the caller method?
1 4 2 3
func getBlockedUsersList() -> [String]{
var blockedList = [String]()
var blockedUsers = [String]()
var blockedByUsers = [String]()
print("1")
DataService.run.getBlockedByUsersList { (success, blockedByUsersList) in
print("2")
if success {
blockedByUsers = blockedByUsersList
DataService.run.getBlockedUsersList(handler: { (success, blockedUsersList) in
print("3")
if success {
blockedUsers = blockedUsersList
blockedList = Array(Set(blockedUsers + blockedByUsers))
}//end if-success
})//getBlockedUsersList
}//end if-success
}//end getBlockedByUsersList
print("4")
return blockedList
}//end func
Function getBlockedByUsersList
gets list from Firebase:
func getBlockedByUsersList(handler: @escaping (Bool, [String]) -> ()){
guard let uid = Auth.auth().currentUser?.uid else { return }
var blockedUIDs = [String]()
REF_USERS.child(uid).child("blocked-by-users").observeSingleEvent(of: .value, with: { (snapshot) in
for child in snapshot.children{
let itemSnap = child as! DataSnapshot
blockedUIDs.append(itemSnap.key)
}//end for
handler(true, blockedUIDs)
}) { (error) in
print("error: \(error.localizedDescription)")
}
}//end func