我正在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);
}