user12180762
user12180762

Reputation:

AddSnapShotListener is repeating the document reading in one instance

When I am using addSnapshotListener for realtime updates, the documents are repeated which should not be the case, but when using getDocuments() the documents are repeated once only, I need to use addSnaphotListener but not want to duplicate the document reading, please assist where I am wrong in using snapshot listener.

I am using Firestore database in Swift iOS. Below is the code I am using

Code with addSnapShotListener():

func getComments() {

      //print(postId + "received")
        let commentsRef = Firestore.firestore().collection("posts").document(postId).collection("comments")

        commentsRef.addSnapshotListener { (snapshot, error) in

            if let error = error {

                print(error.localizedDescription)

            } else {

                if let snapshot = snapshot {

                    for document in snapshot.documents {

                      // self.length = snapshot.count


                        let data = document.data()
                        let username = data["comment_author_username"] as? String ?? ""
                        let comment = data["comment_author_comment"] as? String ?? ""
                        let spinnerC = data["comment_author_spinnerC"] as? String ?? ""
                        let fullname = data["comment_author_fullname"] as? String ?? ""
                        let email = data["comment_author_email"] as? String ?? ""
                        let commentUserImageUrl = data["comment_user_image"] as? String ?? ""
                        let commentuser_id = data["comment_author_id"] as? String ??  ""
                        self.checkl1value = data["l1"] as? Bool


                        let newComment = Comment(_documentId: document.documentID, _commentAuthorUsername: username, _commentAuthorFullName: fullname, _commentAuthorComment: comment, _commentUserImage: commentUserImageUrl, _commentAuthorSpinnerC: spinnerC, _commentAuthorId:commentuser_id, _checkl1value: self.checkl1value)

                        self.comments.append(newComment)
                    //   print(self.length!)

                    }
                   self.tableView.reloadData()
                }
            }
        }
    }

Code With getDocuments():

func getComments() {

      //print(postId + "received")
        let commentsRef = Firestore.firestore().collection("posts").document(postId).collection("comments")

        commentsRef.getDocuments { (snapshot, error) in

            if let error = error {

                print(error.localizedDescription)

            } else {

                if let snapshot = snapshot {

                    for document in snapshot.documents {

                      // self.length = snapshot.count


                        let data = document.data()
                        let username = data["comment_author_username"] as? String ?? ""
                        let comment = data["comment_author_comment"] as? String ?? ""
                        let spinnerC = data["comment_author_spinnerC"] as? String ?? ""
                        let fullname = data["comment_author_fullname"] as? String ?? ""
                        let email = data["comment_author_email"] as? String ?? ""
                        let commentUserImageUrl = data["comment_user_image"] as? String ?? ""
                        let commentuser_id = data["comment_author_id"] as? String ??  ""
                        self.checkl1value = data["l1"] as? Bool


                        let newComment = Comment(_documentId: document.documentID, _commentAuthorUsername: username, _commentAuthorFullName: fullname, _commentAuthorComment: comment, _commentUserImage: commentUserImageUrl, _commentAuthorSpinnerC: spinnerC, _commentAuthorId:commentuser_id, _checkl1value: self.checkl1value)

                        self.comments.append(newComment)
                    //   print(self.length!)

                    }
                   self.tableView.reloadData()
                }
            }
        }
    }

Upvotes: 0

Views: 1315

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

You're probably looking to only handle the changes between the snapshots. To do that you'll want to loop over instead of, as shown in the documentation on viewing changes between snapshots:

db.collection("cities").whereField("state", isEqualTo: "CA")
    .addSnapshotListener { querySnapshot, error in
        guard let snapshot = querySnapshot else {
            print("Error fetching snapshots: \(error!)")
            return
        }
        snapshot.documentChanges.forEach { diff in
            if (diff.type == .added) {
                print("New city: \(diff.document.data())")
            }
            if (diff.type == .modified) {
                print("Modified city: \(diff.document.data())")
            }
            if (diff.type == .removed) {
                print("Removed city: \(diff.document.data())")
            }
        }
    }

Initially your listener will get called with diff.type == .added for each existing document, and then when there are changes it'll get called with the right mix of types.

Upvotes: 5

Related Questions