Firestore分页最后一个可见文档抛出绑定异常 [英] Firestore pagination last visible document throws out of bound exception

查看:74
本文介绍了Firestore分页最后一个可见文档抛出绑定异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已尝试通过使用以下链接使用Firestore分页 https://stackoverflow.com/a/50742175/4232013 但是我在集合中添加了一些文档后突然面对ArrayIndexOutOfBoundsException,谁能帮助我指出我在哪里弄错了,谢谢. 我的代码:

Have tried to use Firestore pagination by using these links https://stackoverflow.com/a/50742175/4232013 but i facing a ArrayIndexOutOfBoundsException suddenly after some added documents in collection can any one help me to point out where i am making a mistake thanks. My code:

 private int limit = 2;
private int PostLimit = 1;

   private void GettingFollowingList() {
    followingList = new ArrayList<>();
    String UserID = FirebaseAuth.getInstance().getUid();
    followingList.add(UserID);

    FirebaseFirestore db = FirebaseFirestore.getInstance();
    db.collection("Users")
            .document(UserID)
            .collection("UserDetails")
            .document(UserID)
            .collection("Following")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot documentSnapshot : task.getResult()) {
                            Followers followers = documentSnapshot.toObject(Followers.class);
                            followingList.add(followers.getFollow_id());
                            Log.d(TAG, "onComplete: FolloIDS: " + followers.getFollow_id());
                        }

                        ReadingPosts();
   

                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });

}

我的分页:

     private void ReadingPosts() {


    String userId = FirebaseAuth.getInstance().getUid();
    FirebaseFirestore db = FirebaseFirestore.getInstance();

    followingList.add(userId);
    final CollectionReference UserRef = db.collection("UserPosts");
    Query query = UserRef.limit(limit);

    query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull final Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot documentSnapshot : task.getResult()) {
                    
                    for (String id : followingList) {
                       

                        if (documentSnapshot.getId().equals(id)) {
                            Log.d(TAG, "onComplete: IDS3 :" + id);
                            FirebaseFirestore db = FirebaseFirestore.getInstance();
                            CollectionReference PostRef = db.collection("UserPosts")
                                    .document(id)
                                    .collection("Posts");

                            Query query = PostRef.orderBy("timestamp", Query.Direction.DESCENDING).limit(PostLimit);

                            query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                @Override
                                public void onComplete(@NonNull Task<QuerySnapshot> task) {

                                    if (task.isSuccessful()) {
                                        for (QueryDocumentSnapshot documentSnapshot1 : task.getResult()) {
                                            Posts posts = documentSnapshot1.toObject(Posts.class);
                                            postsList.add(posts);
                                            
                                        }
                                    }

                                    postsAdapter.notifyDataSetChanged();
                                    swipeRefreshLayout.setRefreshing(false);
                                }
                            })

                                    .addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            Log.d(TAG, "onFailure: Failed to get the posts");
                                        }
                                    });
                        }
                    }

                    postsAdapter.notifyDataSetChanged();
                }

                lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);

                RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {

                    @Override
                    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                        if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                            isScrolling = true;
                        }
                    }

                    @Override
                    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                        int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                        int VisibleItemCount = linearLayoutManager.getChildCount();
                        int TotalItemCount = linearLayoutManager.getItemCount();

                        if (isScrolling && (firstVisibleItemPosition + VisibleItemCount == TotalItemCount) && !isLastItemReached) {
                            isScrolling = false;
                            Query nextQuery = UserRef.startAfter(lastVisible).limit(2);
                            nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                @Override
                                public void onComplete(@NonNull Task<QuerySnapshot> task1) {
                                    if (task1.isSuccessful()) {
                                        for (QueryDocumentSnapshot documentSnapshot : task1.getResult()) {
                                           
                                            for (String id : followingList) {
                                                
                                                if (documentSnapshot.getId().equals(id)) {
                                                    Log.d(TAG, "onComplete: IDS3 :" + id);
                                                    FirebaseFirestore db = FirebaseFirestore.getInstance();
                                                    CollectionReference PostRef = db.collection("UserPosts")
                                                            .document(id)
                                                            .collection("Posts");

                                                    Query query = PostRef.orderBy("timestamp", Query.Direction.DESCENDING).limit(PostLimit);

                                                    query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                                        @Override
                                                        public void onComplete(@NonNull Task<QuerySnapshot> task) {

                                                            if (task.isSuccessful()) {
                                                                for (QueryDocumentSnapshot documentSnapshot1 : task.getResult()) {
                                                                    Posts posts = documentSnapshot1.toObject(Posts.class);
                                                                    postsList.add(posts);
                                                                }
                                                            }

                                                            postsAdapter.notifyDataSetChanged();
                                                        }
                                                    })

                                                            .addOnFailureListener(new OnFailureListener() {
                                                                @Override
                                                                public void onFailure(@NonNull Exception e) {
                                                                    Log.d(TAG, "onFailure: Failed to get the posts");
                                                                }
                                                            });
                                                }
                                            }


                                        }

                                        postsAdapter.notifyDataSetChanged();
                                        lastVisible = task1.getResult().getDocuments().get(task1.getResult().size() - 1);

                                        if (task1.getResult().size() < limit) {
                                            isLastItemReached = true;
                                        }
                                    }
                                }
                            });
                        }

                    }
                };
                post_recycler_view.addOnScrollListener(onScrollListener);


            } else {
                Log.d(TAG, "Error getting documents: ", task.getException());
            }
        }
    });

}

我在此行遇到错误输出

  lastVisible = task1.getResult().getDocuments().get(task1.getResult().size() - 1);

我的日志输出

java.lang.ArrayIndexOutOfBoundsException:length = 0;指数= -1 在java.util.ArrayList.get(ArrayList.java:439)

java.lang.ArrayIndexOutOfBoundsException: length=0; index=-1 at java.util.ArrayList.get(ArrayList.java:439)

推荐答案

错误消息告诉您所获得的结果页面包含0个文档.如果task1.getResult().size()返回0,则从中减去1将为-1,并且您无法使用-1索引到数组中.在进行任何操作之前,应先检查尺寸.

The error message is telling you that the page of results that you got contained 0 documents. If task1.getResult().size() returns 0, then subtracting 1 from that would be -1, and you can't index into an array with -1. You should check the size before you do anything with it.

这篇关于Firestore分页最后一个可见文档抛出绑定异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆