gpt4 book ai didi

java - Firestore 分页

转载 作者:行者123 更新时间:2023-11-29 07:26:57 24 4
gpt4 key购买 nike

我对如何使用 Firestore 正确对查询进行分页有疑问。

通过将下一个查询放入上一个查询的 OnSuccessListener 中,就像 Firestore 页面上的示例一样,是否会不可避免地总是触发一次加载所有页面的链式 react ?这难道不是我们想通过分页来避免的事情吗?

// Construct query for first 25 cities, ordered by population
Query first = db.collection("cities")
.orderBy("population")
.limit(25);

first.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
// ...

// Get the last visible document
DocumentSnapshot lastVisible = documentSnapshots.getDocuments()
.get(documentSnapshots.size() -1);

// Construct a new query starting at this document,
// get the next 25 cities.
Query next = db.collection("cities")
.orderBy("population")
.startAfter(lastVisible)
.limit(25);

// Use the query for pagination
// ...
}
});

来源:https://firebase.google.com/docs/firestore/query-data/query-cursors

这是我的方法。我知道在真实的应用程序中我应该使用 RecyclerView,但我只想在 TextView 上测试它。我想在每次点击按钮时加载 3 个文档。将 lastResult 存储为成员,然后检查它是否不为 null,以查看它是否是第一个查询是否有意义?

public void loadMore(View v) {
Query query;
if (lastResult == null) {
query = notebookRef.orderBy("priority")
.orderBy("title")
.limit(3);
} else {
query = notebookRef.orderBy("priority")
.orderBy("title")
.startAfter(lastResult)
.limit(3);
}
query.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
String data = "";

for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Note note = documentSnapshot.toObject(Note.class);
note.setDocumentId(documentSnapshot.getId());

String documentId = note.getDocumentId();
String title = note.getTitle();
String description = note.getDescription();
int priority = note.getPriority();

data += "ID: " + documentId
+ "\nTitle: " + title + "\nDescription: " + description
+ "\nPriority: " + priority + "\n\n";
}

if (queryDocumentSnapshots.size() > 0) {
data += "_____________\n\n";
textViewData.append(data);


lastResult = queryDocumentSnapshots.getDocuments()
.get(queryDocumentSnapshots.size() - 1);
}
}
});
}

最佳答案

您必须在回收器 View / ListView 中使用 ScrollListener。开始时,您将获取 25 个数据限制,一旦用户再次滚动到页面末尾,您必须使用限制进行新的 firestore 调用(无论您保留什么)。但此时您必须在查询中继续使用 startAt()startAt() 的输入将是第一次获取的数据中的最后一个键。这只是基本概述。您可以引用this查询链接。

您可以使用 firestore 在 recyclerview/listview 中创建分页,如下所示:

基本上遵循以下步骤:

1) 打开 Activity/Fragment 时,您的第一个查询将获取 25 个数据限制

Query first = db.collection("cities")
.orderBy("population")
.limit(25);

first.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
// add data to recyclerView/listview

// Get the last visible document
DocumentSnapshot lastVisible = documentSnapshots.getDocuments()
.get(documentSnapshots.size() -1);
}
});

2) 现在覆盖适配器的 onScrollListener

boolean isEndChildResults = false;
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
isScrolling = true;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
currentVisibleItem = linearLayoutManager.getChildCount();
totalItem = linearLayoutManager.getItemCount();
scrolledItem = linearLayoutManager.findFirstVisibleItemPosition();
if (isScrolling && (currentVisibleItem + scrolledItem == totalItem) && !isEndChildResults && documentSnapshot != null) {
isScrolling = false;
mProgressBarScroll.setVisibility(View.VISIBLE);

FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();

Query query = firebaseFirestore.collection(...).document(...).limit(25).orderBy(...).startAt(lastVisible);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {

if (task.isSuccessful()) {
// add data to recyclerView/listview
lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() -1);


if (task.getResult().size() < postPerPageLimit) {
// if your result size is less than your query size which means all the result has been displayed and there is no any other data to display
isEndChildResults = true;
}
}

}
}
});

if(isEndChildResults){
// show snackbar/toast
}
}

*lastVisible documentSnapshot 将在每次滚动时发生变化,并且将从 lastVisible 快照中获取数据

关于java - Firestore 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50367396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com