I have these two functions. What this does is check if the my firebase realtime database has a users UUID in it or not. Unfortunately I found this code does not work. I thought this would run in order but my function isIDUsed() gets returned before the database has a chance to check if the value exists or not. So it always returns the value inDatabase is initialized in. Which is false
I have tried to implement a semaphore but im new to swift and just cant seem to get the syntax correct. Please let me know if you have a solution thank you
func isIdUsed(ID: String) -> Bool{
var databasedictionary: NSDictionary = NSDictionary()
var inDatabase = false
devindatabse.child("TotalUsers").observeSingleEvent(of: .value, with: { snapshot in
let value = snapshot.value as? NSDictionary
databasedictionary = value! as NSDictionary
if totalUsersCheck(totalUsers: databasedictionary, ID: ID) {
inDatabase = true
}
// ...
}) { error in
print(error.localizedDescription)
}
return inDatabase
}
func totalUsersCheck(totalUsers: NSDictionary, ID: String) -> Bool{
if totalUsers[ID] != nil {
return true
}
return false
}