首页 > 解决方案 > 在 Firestore 中添加新文档时出现 IllegalArgumentException

问题描述

我正在 Firestore 中添加一个新文档。当我返回显示这些文档列表的活动时添加新文档后,我的应用程序崩溃并出现以下错误。在我的查询中,我正在订购带有服务器时间戳的文档。

java.lang.IllegalArgumentException:无效查询。您正在尝试使用字段“additionTime”是未提交的服务器时间戳的文档开始或结束查询。(由于该字段的值未知,因此您无法使用它开始/结束查询。) 在 com.google.firebase.firestore.Query.boundFromDocumentSnapshot(com.google.firebase:firebase-firestore@@21.0.0:742) 在 com.google.firebase.firestore.Query.startAfter(com.google.firebase:firebase -firestore@@21.0.0:632) 在 com.firebase.ui.firestore.paging.PageKey.getPageQuery(PageKey.java:29) 在 com.firebase.ui.firestore.paging.FirestoreDataSource.loadAfter(FirestoreDataSource.java: 106) 在 androidx.paging.PageKeyedDataSource.dispatchLoadAfter(PageKeyedDataSource.java:338) 在 androidx.paging.ContiguousPagedList$3.run(ContiguousPagedList.java:228) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 在 java.lang.Thread.run(Thread.java:761)

它仅在我添加第一个文档时发生。我的 POJO 课在这里

public class BookLight implements Serializable {

    private String bookId;
    private String bookName;
    private String authors;
    private float rating;
    private String category;
    private String thumbnail;
    private BookState bookState;
    private String ownerID;

    @ServerTimestamp
    private Date additionTime;



    public BookLight(){

    }

    //other getters and settes
    public Date getAdditionTime() {
        return additionTime;
    }

    public void setAdditionTime(Date additionTime) {
        this.additionTime = additionTime;
    }


}

在这里我FirestorePagingAdapter用来展示这些项目

private FirestorePagingAdapter<BookLight, BookViewHolder> mAdapter;

private void loadData(){
                query= FirebaseFirestore.getInstance()
                        .collection("Groups")
                        .document(groupDetails.getGroupID())
                        .collection("books")
                        .orderBy("additionTime",Query.Direction.DESCENDING);

                PagedList.Config config = new PagedList.Config.Builder()
                        .setEnablePlaceholders(false)
                        .setPrefetchDistance(5)
                        .setPageSize(3)
                        .build();

                FirestorePagingOptions<BookLight> options = new FirestorePagingOptions.Builder<BookLight>()
                        .setLifecycleOwner(this)
                        .setQuery(query, config, BookLight.class)
                        .build();

                mAdapter = new FirestorePagingAdapter<BookLight, BookViewHolder>(options) {
                    @NonNull
                    @Override
                    public BookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                        return new BookViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.book_row_item, parent, false));
                    }

                    @Override
                    protected void onBindViewHolder(@NonNull BookViewHolder bookViewHolder, int position, @NonNull final BookLight book) {
                    }


                    @Override
                    protected void onError(@NonNull Exception e) {
                        super.onError(e);
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                };

                recyclerView.setAdapter(mAdapter);
}

数据库结构 在此处输入图像描述

标签: androidfirebasegoogle-cloud-firestorefirebaseuiandroid-paging

解决方案


推荐阅读