是否可以从Firestore中的另一个集合访问一个集合中的字段值?

时间:2019-03-09 15:02:52

标签: android firebase kotlin google-cloud-firestore

是否可以将Allusers集合的字段值(在这种情况下为gamerroomname)从另一个集合(在这种情况下为gameroom)访问/传递?

以便我可以在下面进行操作:

ref.collection("GameRooms").document(gameroomname).addSnapshotListener(){
}

所有用户的屏幕截图: enter image description here

游戏室截图:

enter image description here

尝试之后,下面的代码可以工作。但这是正确的方法还是有更好的方法:

 mFirestore.collection("AllUsers").document(SplitString(currentUser.email!!)).get().addOnSuccessListener(OnSuccessListener<DocumentSnapshot> { documentSnapshot ->
            var gameRoomName: String = ""
            if (documentSnapshot.exists()) {
                gameRoomName = documentSnapshot.getString("gameroomname")!!
            }
            mFirestore.collection("GameRooms").document(gameRoomName).collection("Words called").addSnapshotListener(EventListener<QuerySnapshot>(){ queryDocumentSnapshot, e->
                selectedWordsList.clear()
                if (!queryDocumentSnapshot!!.isEmpty) {
                    val gameRoomList = queryDocumentSnapshot.documents
                    for (doc: DocumentSnapshot in gameRoomList) {
                        var addWord: String = ""
                        if (doc.get("addword") != null) {
                            addWord = doc.get("addword").toString()
                        }
                        val selectedGameRoomList: SelectedWordsObject = SelectedWordsObject(addWord, false)
                        selectedWordsList.add(selectedGameRoomList)
                        mSelectedWordsAdapter.notifyDataSetChanged()
                    }
                    mSelectedWordsAdapter.notifyItemChanged(0)
                }
                else {
                    Toast.makeText(this@Screen, "Document does not exist", Toast.LENGTH_LONG).show()
                }
            })
        }).addOnFailureListener(OnFailureListener { e->
            val error=e.message
            Toast.makeText(this@Screen,"Error:"+error, Toast.LENGTH_LONG).show()
        })

0 个答案:

没有答案