-1

I fetch data and display in a tableView, the problem is the data is not executing in the correct order.

I have tried:

            for case let child as DataSnapshot in data!.children.reversed() {
                let newDispatchGroup = DispatchGroup()
                let commentID = child.key
                let uid = child.childSnapshot(forPath: "UID").value as! String
                let commentText = child.childSnapshot(forPath: "Comment").value!
                let timeStamp = child.childSnapshot(forPath: "timeStamp").value!
                let date = ConvertDate(mediaTimestamp: timeStamp as! Double).getDate!
                //print(date, "dsfsdafdasfdsafdsahjkfhfdsafsajkadhffdsfsafsasjkfhsdajkhfdsajkhfjklads")

                newDispatchGroup.enter()

                ref.child("users2").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
                    print(snapshot, "dshjkfhjkadhfsjkfhsdajkhfdsajkhfjklads")
                    print(date, "dsfsdafdasfdsafdsahjkfhfdsafsajkadhffdsfsafsasjkfhsdajkhfdsajkhfjklads")

                    let username = snapshot.childSnapshot(forPath: "username").value
                    let profileImage = snapshot.childSnapshot(forPath: "profileImage").value

                    let newUser = User(theuserID: uid, theUsername: username as! String, theprofImage: profileImage as! String)

                    let newComment = Comment(newUser: newUser, text: commentText as! String, timeStamp: date, NcommentID: commentID)
                    self.commentsVC1.arrayOfComments.append(newComment)
                    newDispatchGroup.leave()
                    //completion()
                })
                newDispatchGroup.notify(queue: .main, execute: {
                    print(self.totalComments, "COgfdsdfgfdsgdsfgdfsgfdsgdfsgdskj", self.commentsVC1.arrayOfComments.count)
                    if self.totalComments == self.commentsVC1.arrayOfComments.count {
                        print("COmejkfbdshkafdsagfhksdagfdsakj")
                        self.commentsVC1.tableView.reloadData()
                    }
                })
            }
        })
    }

But it did not work either, the order in which the second firebase calls execute is incorrect.

2 Answers2

0

You should set up your notify closure when you set up your DispatchGroup. And you would not need to use a completion closure for you loadComments function.

let dispatchGroup = DispatchGroup()
dispatchGroup.notify(queue: .main, execute: {
    if self.totalComments == self.commentsVC1.arrayOfComments.count {
        print("COmejkfbdshkafdsagfhksdagfdsakj")
        self.commentsVC1.tableView.reloadData()
    }
})

loadComments()

notify will be called, when leave has been called the same amount of times as enter. In your code the last leave call is happening before you have set the anything to be notified about.

juhan_h
  • 3,965
  • 4
  • 29
  • 35
  • I tried this (passing in as argument) and it did not work –  Jul 17 '19 at 02:50
  • Also the notify only runs 1 time doing this. i dont know why –  Jul 17 '19 at 03:04
  • The notify will _only_ run once. That is the point of a DispatchGroup - you create it, you use it and then you dispose of it. If you need to do something else, you might need to consider a different approach. – juhan_h Jul 17 '19 at 09:27
  • You can use your updated answer and it might work, if you will use separate DispatchGroups: an outer one and an inner one. The inner ones would call outer's "enter" when are being created, and their "notify" will call "outer's" "leave". – juhan_h Jul 17 '19 at 09:31
0

I solved with this:

    for case let child as DataSnapshot in snap.children.reversed() {
                    let commentID = child.key
                    let uid = child.childSnapshot(forPath: "UID").value as! String
                    let commentText = child.childSnapshot(forPath: "Comment").value!
                    let timeStamp = child.childSnapshot(forPath: "timeStamp").value!
                    let date = ConvertDate(mediaTimestamp: timeStamp as! Double).getDate!
                    let newUser = User(theuserID: uid)
                    let newComment = Comment(newUser: newUser, text: commentText as! String, timeStamp: date, NcommentID: commentID)
                    self.commentsVC1.arrayOfComments.append(newComment)
                    ref.child("users2").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in

                        let username = snapshot.childSnapshot(forPath: "username").value
                        let profileImage = snapshot.childSnapshot(forPath: "profileImage").value

                        let newUserIner = User(theuserID: uid, theUsername: username as! String, theprofImage: profileImage as! String)
                        newComment.user = newUserIner
                        if self.totalComments == self.commentsVC1.arrayOfComments.count {
                            self.commentsVC1.tableView.reloadData()
                        }
                    })
                }

I would use dispatch group here though so one does not have have to check if its done unnecessarily.