I am trying to set a helper function. This one should return the results of search history. But it returns an empty Array always. The print in Console shows me a full array. It seems like, the return executed to early.
static func getSearchHistorySorted(userID: String) -> [String]{
var searchHistory = [String]()
databaseReference.child("\(userID)/searchhistory").queryOrderedByValue().queryLimited(toFirst: 6).observeSingleEvent(of: .value, with: { (snapshot) in
if let entries = snapshot.value as? [String : Double] {
let myArr = Array(entries.keys)
let sortedKeys = myArr.sorted() {
let obj1 = entries[$0] // get ob associated w/ key 1
let obj2 = entries[$1] // get ob associated w/ key 2
return obj1! < obj2!
}
searchHistory = sortedKeys
print(searchHistory)
}
})
return searchHistory
}