I am trying to query the most popular hashtags in my database. The most popular hashtag has the most posts. First, this is how my Hashtags database looks like
Hashtags L firstHashtag L post1: true L post2: true L post3: true L post_count: 3 L secondHashtag L post4: true L post5: true L post6: true L post7: true L post8: true L post_count: 5 L thirdHashtag L post9: true L post_count: 1 I query their order based on their post count and fetch the 8 most popular hashtags like this:
func observePopularTags(completion: @escaping (String) -> Void) { REF_HASHTAG.queryOrdered(byChild: "post_count").queryLimited(toLast: 8).observe(.childAdded, with: { snapshot in completion(snapshot.key) }) } Then I add them to an empty array
var hashtags: [String] = [] After that I call a function to observe & insert the popular hashtags in the array
func getHashtags() { Api.HashTag.observePopularTags { (name) in let hashtag = "#" + name // This works, it prints out each popular hashtag print(hashtag) self.hashtags.append(hashtag) } // When I print out the whole array, it prints an empty array. print("Hashtags: ", hashtags) } I call this function in the viewDidLoad method. However, when I print the array, it prints it out as an empty array. Why is this happening?