【问题标题】:Firestore - get specific document based on the field?Firestore - 根据字段获取特定文档?
【发布时间】:2019-05-28 09:00:07
【问题描述】:
 mFirestore.collection("FeaturedDeal").whereEqualTo("title","Amazon India").limit(10).orderBy("priority", Query.Direction.ASCENDING).addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@javax.annotation.Nullable QuerySnapshot documentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
                if (e != null) {
                    Log.d(TAG, "Error : " + e.getMessage());

                }

                    assert documentSnapshots != null;
                    for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                        if (doc.getType() == DocumentChange.Type.ADDED) {
                            String doc_id = doc.getDocument().getId();
                            FeaturedDeal featuredDeal = doc.getDocument().toObject(FeaturedDeal.class).withDocId(doc_id);
                            featuredDeals.add(featuredDeal);
                            featuredDealAdapter.notifyDataSetChanged();
                        } else if (doc.getType() == DocumentChange.Type.MODIFIED) {
                            String docID = doc.getDocument().getId();
                            FeaturedDeal changedModel = doc.getDocument().toObject(FeaturedDeal.class).withDocId(docID);
                            if (doc.getOldIndex() == doc.getNewIndex()) {
                                // Item changed but remained in same position
                                featuredDeals.set(doc.getOldIndex(), changedModel);
                                featuredDealAdapter.notifyItemChanged(doc.getOldIndex());
                            } else {
                                // Item changed and changed position
                                featuredDeals.remove(doc.getOldIndex());
                                featuredDeals.add(doc.getNewIndex(), changedModel);
                                featuredDealAdapter.notifyItemMoved(doc.getOldIndex(), doc.getNewIndex());
                            }
                        } else if (doc.getType() == DocumentChange.Type.REMOVED) {
                            // remove
                            featuredDeals.remove(doc.getOldIndex());
                            featuredDealAdapter.notifyItemRemoved(doc.getOldIndex());
                        }
                    }
                }

        });

应用程序崩溃并将此错误输入 Logcat:

java.lang.NullPointerException:尝试在 com.example.info.lootbox.Fragments 的空对象引用上调用虚拟方法“java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()”。 HomeFragment$9.onEvent(HomeFragment.java:313)

【问题讨论】:

    标签: java android firebase firebase-realtime-database google-cloud-firestore


    【解决方案1】:

    删除这个:

    assert documentSnapshots != null;
    

    并在您的侦听器中添加此检查而不是断言:

    if (e != null) {
      Log.w(TAG, "listen:error", e);
      return;
    }
    

    如果有错误,你应该在监听器中返回。错误表明快照中没有数据。

    【讨论】:

    • .orderBy("priority", Query.Direction.ASCENDING) 不起作用。如果我输入这行代码,我的应用程序不会在我对文档进行更改时实时更新数据。我必须关闭我的应用程序并重新启动它才能看到这些更改。如果我删除这行代码,我的应用程序可以正常工作。并在对数据库进行更改时实时更新数据。但我也想按 orderBy 过滤数据
    【解决方案2】:

    您必须首先检查快照中是否有任何可用数据。所以像这样检查。

    mFirestore.collection("FeaturedDeal").whereEqualTo("title","Amazon India").limit(10).orderBy("priority", Query.Direction.ASCENDING).addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@javax.annotation.Nullable QuerySnapshot documentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
                    if (e != null) {
                        Log.d(TAG, "Error : " + e.getMessage());
                       return;
                    }else if ( documentSnapshots != null) {
    
                     for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
    
                            if (doc.getType() == DocumentChange.Type.ADDED) {
                                String doc_id = doc.getDocument().getId();
                                FeaturedDeal featuredDeal = 
                doc.getDocument().toObject(FeaturedDeal.class).withDocId(doc_id);
                                featuredDeals.add(featuredDeal);
                                featuredDealAdapter.notifyDataSetChanged();
                            } else if (doc.getType() == DocumentChange.Type.MODIFIED) {
                                String docID = doc.getDocument().getId();
                                FeaturedDeal changedModel = 
                doc.getDocument().toObject(FeaturedDeal.class).withDocId(docID);
                                if (doc.getOldIndex() == doc.getNewIndex()) {
                                    // Item changed but remained in same position
                                    featuredDeals.set(doc.getOldIndex(), changedModel);
                                    featuredDealAdapter.notifyItemChanged(doc.getOldIndex());
                                } else {
                                    // Item changed and changed position
                                    featuredDeals.remove(doc.getOldIndex());
                                    featuredDeals.add(doc.getNewIndex(), changedModel);
                                    featuredDealAdapter.notifyItemMoved(doc.getOldIndex(), doc.getNewIndex());
                                }
                            } else if (doc.getType() == DocumentChange.Type.REMOVED) {
                                // remove
                                featuredDeals.remove(doc.getOldIndex());
                                featuredDealAdapter.notifyItemRemoved(doc.getOldIndex());
                            }
                        }
                    }
    
               }
    
            });
    

    【讨论】:

    • .orderBy("priority", Query.Direction.ASCENDING) 不起作用。如果我输入这行代码,我的应用程序不会在我对文档进行更改时实时更新数据。我必须关闭我的应用程序并重新启动它才能看到这些更改。如果我删除这行代码,我的应用程序可以正常工作。并在对数据库进行更改时实时更新数据。但我也想按 orderBy 过滤数据
    猜你喜欢
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 2023-02-13
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    相关资源
    最近更新 更多