Firestore返回功能而不将任何对象附加到数组

时间:2018-01-17 12:49:11

标签: swift google-cloud-firestore

由于某些原因,我添加了代码if user.uid == uid {return}    它将完全退出函数并且不会将任何用户添加到数组中,即使我只想过滤掉当前用户添加到数组中吗?

func fireStoreFetchUsers(){
    guard let uid = Auth.auth().currentUser?.uid else { return }
    let db = Firestore.firestore()

    db.collection("Users")
       .getDocuments() { (querySnapshot, err) in

          if let err = err {
            print("Error getting documents: \(err)")
          } else {
            for document in querySnapshot!.documents {
                let dictionary = document.data()
                let user = User2(uid: "", distanceFrom: "any", dictionary: dictionary)

                if user.uid == uid {return}

                self.users.append(user)               
                self.collectionView?.reloadData()
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

总结您的问题似乎是想要跳过正在迭代的数组的特定元素。这正是continue所做的,所以只需将return更改为continue

在上述更改之后,循环的最后两行不会在user.uid == uid时执行,因此该特定用户不会被添加到users数组并且collectionView不会重新加载,但循环将继续使用下一个元素进行迭代,而不是从函数返回。

for document in querySnapshot!.documents {
    let dictionary = document.data()
    let user = User2(uid: "", distanceFrom: "any", dictionary: dictionary)

    if user.uid == uid {continue}

    self.users.append(user)               
    self.collectionView?.reloadData()
}