Friday, 25 October 2019

Table view in segmented control doesn’t show data immediately

I've got a table view under a segmented control, the table view's data is retrieved from Firebase database, here is the code:

func retrieveParticipatedData(completion: @escaping (Bool) -> Void) {

    let storyIDRef = DBRef.child("Story IDs").child(userID!)
    var participatedStoryItems: [ParticipatedStoriesPageData] = []

    storyIDRef.observeSingleEvent(of: .value) { (snapshot) in
        if let dict = snapshot.value as? [String : String] {

            for (_, value) in dict {

                self.DBRef.child("To be reviewed stories").child(value).observeSingleEvent(of: .value) { (snapshot) in
                    if let storyDict = snapshot.value as? [String : Any] {

                        let storyTitle = storyDict["Book Title"] as? String
                        let storyDescription = storyDict["Description"] as? String
                        let storyID = value
                        let votes = storyDict["Votes"] as? Int

                        let story = ParticipatedStoriesPageData(storyKey: storyID, storyTitle: storyTitle!, storyDescription: storyDescription!, votes: votes!)
                        participatedStoryItems.append(story)
                    }
                    self.participatedStories = participatedStoryItems
                    completion(true)
                }
            }
        }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    retrieveParticipatedData { (isRetreived) in

        if isRetreived {
            self.tableView.isHidden = false
            self.tableView.reloadData()
        }   
    }
}

There is no issue with the retrieval of the data. The table view and the segmented control is initially hidden and once the data is retrieved, I unhide them.

The problem is that the data in the table view isn't displayed immediately, I have to go to the other tab and come back (I've got 2 tabs in my segmented control), only then the data is displayed in the table view. I have also added a picture of the database.

Database

Participants database

Update

Here is what I tried with Matt's help:

func retrieveParticipatedData() {

    let dispatchGroup = DispatchGroup()
    let storyIDRef = DBRef.child("Story IDs").child(userID!)
    var participatedStoryItems: [ParticipatedStoriesPageData] = []

    storyIDRef.observeSingleEvent(of: .value) { (snapshot) in
        if let dict = snapshot.value as? [String : String] {
            for (_, value) in dict {
                self.DBRef.child("To be reviewed stories").child(value).child("Participants").child("Chapter 1 Author").observeSingleEvent(of: .value) { (snapshot) in

                    if snapshot.value as? String == self.userID {
                        dispatchGroup.enter()

                        self.DBRef.child("To be reviewed stories").child(value).observeSingleEvent(of: .value) { (snapshot) in
                            if let storyDict = snapshot.value as? [String : Any] {

                                let storyTitle = storyDict["Book Title"] as? String
                                let storyDescription = storyDict["Description"] as? String
                                let storyID = value
                                let votes = storyDict["Votes"] as? Int

                                let story = ParticipatedStoriesPageData(storyKey: storyID, storyTitle: storyTitle!, storyDescription: storyDescription!, votes: votes!)
                                participatedStoryItems.append(story)
                            }
                            self.participatedStories = participatedStoryItems.reversed()
                            dispatchGroup.leave()
                        }
                        dispatchGroup.notify(queue: .main) {
                            print("Retrieved")
                            self.tableView.reloadData()
                        }
                        // It works if I do this
                        /*dispatchGroup.notify(queue: .main) {
                            print("Retrieved")
                            self.setupTableView // Creates the table view
                        }*/
                        //But I don't wanna do this cuz then there would be no table view if there is no data retreived.
                    }
                }
            }
        }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    retrieveParticipatedData()
    setupStackView()
    setupTableView()  // Creates the table view
}


from Table view in segmented control doesn’t show data immediately

No comments:

Post a Comment