簡體   English   中英

如何使用 Android 在 RecyclerView 中顯示來自 Firestore 的文檔及其子集合?

[英]How to display a document with its subcollections from Firestore in a RecyclerView with Android?

這是我的 Firestore 數據庫的樣子:

在此處輸入圖像描述

我有一個顯示帖子列表的 RecyclerView。 但是我無法檢索包含其子集合的文檔並將其放入 object 中。當我在我的RecyclerView中提交帖子列表時,我希望每個帖子都已經有它的喜歡和評論。

我使用 AsyncTask 來完成查詢用戶所有帖子的后台工作。 AsyncTask 完成后,它會將帖子列表提交給 RecyclerView。 (還是不行)

在下面的代碼中,我調用了用戶的所有帖子,然后獲取每個帖子的“贊”。 我在每篇文章中放置了一個 while 循環,這樣我就可以等待它完成對贊的查詢,然后再繼續下一篇文章快照

doInBackground()

final List<Post> postList = new ArrayList<>();
// -- GET all the posts of the user
feedsCollection.document(documentID).collection("Posts").get()
    .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

            for(final DocumentSnapshot documentSnapshot1 : queryDocumentSnapshots){
                final Post post = documentSnapshot1.toObject(Post.class);

                // Finish getting the likes&comments before proceeding
                while(post.isStillLoading()){
                    // -- GET Likes of each post
                    db.collection("FeedsCollection").document(documentID).collection("Posts")
                            .document(documentSnapshot1.getId()).collection("Likes").get()

                            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                                @Override
                                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                                    List<Like> likeList = new ArrayList<>();
                                    for(DocumentSnapshot likeSnapshot : queryDocumentSnapshots){
                                        Like like = likeSnapshot.toObject(Like.class);
                                        likeList.add(like);
                                    }
                                    post.setLikes(likeList);
                                    post.setStillLoading(false);

                                }
                            });
                    // -- GET comments code
                }
                postList.add(post);
            }
        }
    });
return postList;

onPostExecute()

    protected void onPostExecute(List<Post> postList) {
                super.onPostExecute(postList);
    
                adapter.submitData(postList);
    
    
            }

這些是我嘗試過的事情:

  • 我嘗試在我的 recyclerview 中使用多個視圖類型,這樣如果列表仍然是空的,它會顯示“無項目”。 然后當 AsyncTask 完成時,它會重新提交一個列表到 recyclerview。
  • 我嘗試使用 FirestoreRecyclerAdapter 但我讀到它的查詢很淺,所以它無法完全獲取每個帖子文檔的 Likes 和 Comments 子集合。

還有其他解決方法嗎?

但是我無法檢索包含其子集合的文檔並將其放入 object 中。

你不可能做到這一點。 Firestore 中的查詢很淺,因此只允許從運行查詢的集合中獲取文檔。 此外,您無法在單個 go 中獲取包含嵌套子集合的文檔。您可以獲取文檔,然后對每個子集合運行單獨的查詢。

另一種可能的解決方案可能是使用Firebase Realtime Database ,當您下載一個節點時,您將它與該節點下存在的所有數據一起下載。

我使用 AsyncTask 來完成查詢用戶所有帖子的后台工作。

Cloud Firestore 客戶端已在后台線程中運行 all.network 操作。 這意味着所有操作都不會阻塞您的主線程。 將它放在AsyncTask中不會帶來任何額外的好處。

如果你有多個 collections 中的數據,甚至是嵌套的 collections,你將不得不執行多個查詢來獲取所有這些數據。 僅僅一個查詢是不可能的。 你當然可以實現你想要的,只是這樣做要復雜得多。

它將需要一個查詢來獲取 PostsCollection 中的所有文檔,然后對每個帖子的 Comments 和 Likes 下嵌套的每個子集合進行更多查詢。

按照 Alex 的建議切換到實時數據庫可能不是一個好主意。 你將失去很多更復雜的查詢能力,只是為了獲得深度查詢,你甚至可能不希望一直進行完全深度查詢,因為這總體上可能更昂貴。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM