0
votes

I have an array that loads all users into a user list array and I'm wondering if each each time the array gets loaded, it counts as 1 read or does every element in the array count was its own read?

    var userList = [User]()

    func addUserObserver(_ update: @escaping () -> Void) {
        FriendSystem.system.USER_REF.getDocuments { snapshot, error in
            self.userList.removeAll()
             
            guard error == nil else {
              #if DEBUG
                print("Error retreiving collection")
              #endif
              return
            }
             
            let group = DispatchGroup()
             
            for document in snapshot!.documents {
                let email = document.get("email") as! String
                let username = document.get("username") as! String
                group.enter()
                if email != Auth.auth().currentUser?.email! {
                    self.userList.append(User(userEmail: email, userID: document.documentID, userName: username))
                    group.leave()
                }
            }
             
            group.notify(queue: .main) {
              update()
            }
        }
    }
1
dont add group.leave() in if condition .. your application can crash - Jawad Ali

1 Answers

0
votes

Here

        let group = DispatchGroup()
         
        for document in snapshot!.documents {
            let email = document.get("email") as! String
            let username = document.get("username") as! String
            group.enter()
            if email != Auth.auth().currentUser?.email! {
                self.userList.append(User(userEmail: email, userID: document.documentID, userName: username))
                group.leave()
            }
        }
         
        group.notify(queue: .main) {
          update()
        }

completely you don't need a DispatchGroup as the whole lines are synchronous , also you have only 1 read for all users here FriendSystem.system.USER_REF.getDocuments { snapshot, error in the count of elements inside that read is irrelevant