Swift Firebase - 使用文档UID获取当前用户以填充列表

时间:2018-05-01 13:01:53

标签: ios swift firebase google-cloud-firestore

所以我在我的Android应用程序上做了这个(并且它有效),用一个集合中的文档名称填充列表

  db.collection("usersAuth/${FirebaseAuth.getInstance().uid!!}/KitLists")
            .addSnapshotListener(EventListener<QuerySnapshot> { value, e ->
        if (e != null) {
            Log.w("TAG", "Listen failed.", e)
            return@EventListener
        }

        for (document in value.documents) {
            val data = document

            val kitName = data.id

            firstKitList.add(kitName)

        }

        mainListViewAdapter.notifyDataSetChanged()
    })

我试图在我的iOS版本上做同样的事情,但我不知道什么是错的

override func viewWillAppear(_ animated: Bool) {

    setListener()

}

func setListener() {

    db.collection("usersAuth/\(String(describing: Auth.auth().currentUser))/KitLists")
        .addSnapshotListener { (snapshot, error ) in
            if let err = error {
                debugPrint("Error fetching docs: \(err)")
            } else {
                guard let snap = snapshot else {return}
                for document in snap.documents {
                    let data = document.data()
                    let kitListName = data["KitLists"] as? String


                 let newLists = KitList(kitListName: kitListName!)
                    self.lists.append(newLists)
                }


                self.tableView.reloadData()
            }
    }

}

任何想法?感谢

- 编辑

Firestore

Firestore2

1 个答案:

答案 0 :(得分:0)

您需要从uid获取currentUser,例如:

if let userId = Auth.auth().currentUser.uid {
        db.collection("usersAuth").document(userId).collection("KitLists")
            .addSnapshotListener { (snapshot, error ) in
        //...
    }

获取KitLists documentId

for document in snap.documents {
    let documentName = document.documentID // <--- This
    let newLists = KitList(kitListName: documentName)
    self.lists.append(newLists)
}