قالب وردپرس درنا توس
Home / IOS Development / Ios – Data is grouped in a strange way

Ios – Data is grouped in a strange way



Update:

I think this problem is related to having a fire base call within another fire base call, but I have to do this in order that I do not know each user uid, and then I solve this problem?

I am trying to fix a problem in a table view where data added is grouped by the user (not in the correct order) I was going to fix this by shipping queue, but based on my experiment, the problem continues.

What am I doing wrong?

Note: Shipping queue is declared elsewhere but done in the usual way …

The function that retrieves comments:

      func loadComments (_ completion: @escaping (() -> ( )))
print ("Get comments", "Number of media:
print ("Download cfdsfdsafdasfsafasf4omments", self.arrayOfComments.count)

let ref = Database. database (). reference ()
self.arrayOfComments.removeAll ()
ref.child ("Comments"). ordered (.user.userID selectedPost!). the child (selectedPost! .media.postID!). Child ("media (numberMedia + 1
)") .queryOrdered (byChild: "timeStamp") // try "/" .observeSingleEvent (by: .value) {(snap) i print (snap, "this is the snapshot of the comment") self.totalComments + = snap.children.allObjects.count for the case, let children as DataSnapshot in snap.children.reversed () { leave commentID = child.key la uid = child.childSnapshot (forPath: "UID"). value like! string leave commentText = child.childSnapshot (forPath: "Comment"). value! let timeStamp = child.childSnapshot (forPath: "timeStamp"). value! self.dispatchGroup.enter () ref.child ("users2"). child (uid) .observeSingleEvent (by: .value, with: {(still image) i print (still image, "dshjkfhjkadhfsjkfhsdajkhfdsajkhfjklads") leave username = snapshot.childSnapshot (forPath: "username"). value la profileImage = snapshot.childSnapshot (forPath: "profileImage"). value let newUser = User (theuserID: uid, username: username as! String, theprofImage: profileImage as! String) la date = ConvertDate (mediaTimestamp: timeStamp as! Double) .getDate! la newComment = Comment (newUser: newUser, text: commentText as! String, timeStamp: date, NcommentID: commentID) self.commentsVC1.arrayOfComments.append (newComment) self.dispatchGroup.leave () completion () }) } // if let snapshots = snap.children.allObjects as? [DataSnapshot] { // for children in snapshots.reversed () { // print (child, "fdshfjdsahfljkdsahfklsadf") // leave commentID = child.key // let uid = child.childSnapshot (forPath: "UID"). value like! string // print (uid, "This is the uid") // print (snap, "This is a snapshot! @ ## $") // self.dispatchGroup.enter () // ref.child ("users2"). child (uid) .observeSingleEvent (by: .value, with: {(still image) i // print (snapshot, "dshjkfhjkadhfsjkfhsdajkhfdsajkhfjklads") // // leave user name = snapshot.childSnapshot (forPath: "username"). value // la profileImage = snapshot.childSnapshot (forPath: "profileImage"). value // // add newUser = User (theuserID: uid, username: username as! String, theprofImage: profileImage as! String) // // leave commentText = child.childSnapshot (forPath: "Comment"). value! // la timeStamp = child.childSnapshot (forPath: "timeStamp"). value! // print (timeStamp, "This is teh timeStamp") // // if timeStamp is NSNull { // print ("timestamp is NSNull") // // self.commentsVC1.arrayOfComments.removeAll () // // self.arrayOfComments.removeAll () // // self.loadComments () {} //} other { // la date = ConvertDate (mediaTime: timeStamp if! Double) .getDate! // // la newComment = Comment (newUser: newUser, text: commentText as! String, timeStamp: date, NcommentID: commentID) // // self.arrayOfComments.append (newComment) // self.commentsVC1.arrayOfComments.append (newComment) // print (self.arrayOfComments.count, ": comments added") // // self.commentsVC1.tableView.reloadData () // self.dispatchGroup.leave () // completion () //} //}) //} //} } }

It is called as seen below:

      self.loadComments ({
print ("compelted?")
// self.commentsVC1.configureTableView ()
self.dispatchGroup.notify (queue: .main, execute: {
if self.totalComments == self.commentsVC1.arrayOfComments.count {
out ("COmejkfbdshkafdsagfhksdagfdsakj")
self.commentsVC1.tableView.reloadData ()
}
})
})

Update:

I've also tried something like:

      {// _ completion: @escaping (() -> ())
print ("Get comments", "Number of media:
print ("Download cfdsfdsafdasfsafasf4omments", self.arrayOfComments.count)
var data: DataSnapshot?
let ref = Database. database (). reference ()
self.arrayOfComments.removeAll ()
la newDispatchQueue = DispatchGroup ()
newDispatchQueue.enter ()
ref.child ("Comments"). ordered (.user.userID selectedPost!). the child (selectedPost! .media.postID!). Child ("media (numberMedia + 1)")
.queryOrdered (byChild: "timeStamp") // try "/"
.observeSingleEvent (by: .value) {(snap) i
print (snap, "this is the snapshot of the comment")

self.totalComments + = snap.children.allObjects.count
data = snap
newDispatchQueue.leave ()
// if let snapshots = snap.children.allObjects as? [DataSnapshot] {
// for children in snapshots.reversed () {
// print (child, "fdshfjdsahfljkdsahfklsadf")
// leave commentID = child.key
// let uid = child.childSnapshot (forPath: "UID"). value like! string
// print (uid, "This is the uid")
// print (snap, "This is a snapshot! @ ## $")
// self.dispatchGroup.enter ()
// ref.child ("users2"). child (uid) .observeSingleEvent (by: .value, with: {(still image) i
// print (snapshot, "dshjkfhjkadhfsjkfhsdajkhfdsajkhfjklads")
//
// leave user name = snapshot.childSnapshot (forPath: "username"). value
// la profileImage = snapshot.childSnapshot (forPath: "profileImage"). value
//
// add newUser = User (theuserID: uid, username: username as! String, theprofImage: profileImage as! String)
//
// leave commentText = child.childSnapshot (forPath: "Comment"). value!
// la timeStamp = child.childSnapshot (forPath: "timeStamp"). value!
// print (timeStamp, "This is teh timeStamp")
//
// if timeStamp is NSNull {
// print ("timestamp is NSNull")
// // self.commentsVC1.arrayOfComments.removeAll ()
// // self.arrayOfComments.removeAll ()
// // self.loadComments () {}
//} other {
// la date = ConvertDate (mediaTime: timeStamp if! Double) .getDate!
//
// la newComment = Comment (newUser: newUser, text: commentText as! String, timeStamp: date, NcommentID: commentID)
// // self.arrayOfComments.append (newComment)
// self.commentsVC1.arrayOfComments.append (newComment)
// print (self.arrayOfComments.count, ": comments added")
// // self.commentsVC1.tableView.reloadData ()
// self.dispatchGroup.leave ()
// completion ()
//}
//})
//}
//}
}
newDispatchQueue.notify (queue: .main, execute: {
print (data;
for the case, allow children as DataSnapshot in data! .children.reversed () {
la newDispatchGroup = DispatchGroup ()
leave commentID = child.key
la uid = child.childSnapshot (forPath: "UID"). value like! string
leave commentText = child.childSnapshot (forPath: "Comment"). value!
let timeStamp = child.childSnapshot (forPath: "timeStamp"). value!
la date = ConvertDate (mediaTimestamp: timeStamp as! Double) .getDate!
// print (date, "dsfsdafdasfdsafdsahjkfhfdsafsajkadhffdsfsafsasjkfhsdajkhfdsajkhfjklads")

newDispatchGroup.enter ()

ref.child ("users2"). child (uid) .observeSingleEvent (by: .value, with: {(still image) i
print (still image, "dshjkfhjkadhfsjkfhsdajkhfdsajkhfjklads")
print (date, "dsfsdafdasfdsafdsahjkfhfdsafsajkadhffdsfsafsasjkfhsdajkhfdsajkhfjklads")

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

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

la 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, "COgfdsdfgfsfgdfsgfdsgdfsgdfsgdskj", self.commentsVC1.arrayOfComments.count)
if self.totalComments == self.commentsVC1.arrayOfComments.count {
out ("COmejkfbdshkafdsagfhksdagfdsakj")
self.commentsVC1.tableView.reloadData ()
}
})
}
})
}

But it didn't work either, the order in which the second fire base call runs out is wrong.


Source link