![](/img/trans.png)
[英]Firestore Pagination : StartAfter query with DocumentSnapshot not working
[英]Firestore pagination startAfter(DocumentSnapshot) working in java but not in kotlin
我正在嘗試將一個 Android Firestore 項目從 java 轉換為 kotlin。 但是卡在分頁部分,其中帶有 java 代碼的 startAfter(DocumentSnapshot) 工作正常。 但是 kotlin 只給出了前 3 個結果。 StartAfter(DocumentSnapshot) 部分不起作用。
如果您能指出我在 Kotlin 中哪里出錯了,那將非常有幫助。
這是運行完美的java代碼
public void loadNotes(View v) {
Query query;
if (lastResult == null) {
query = notebookRef.orderBy("priority")
.limit(3);
} else {
query = notebookRef.orderBy("priority")
.startAfter(lastResult)
.limit(3);
}
Log.d(TAG, "loadNotes: "+ query);
query.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
String data = "";
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Notee note = documentSnapshot.toObject(Notee.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);
}
}
});
}
這是Kotlin 的,它不起作用。
private fun loadNotes() {
val query = if (lastResult == null) {
notebookRef
.orderBy("priority")
.limit(3)
} else {
Log.d(TAG, "loadNotes: ${lastResult!!.id}")
notebookRef
.orderBy("priority")
.startAfter(lastResult)
.limit(3)
}
Log.d(TAG, "loadNotes: $query")
query.get()
.addOnSuccessListener { QuerySnapshot ->
var text = ""
for (queryDocumentSnapshot in QuerySnapshot) {
val note: Note = queryDocumentSnapshot.toObject(Note::class.java)
note.docID = queryDocumentSnapshot.id
val title = note.title
val description = note.description
val priority = note.priority
text += "ID: ${note.docID} \n Title : $title\n Description :$description\n" +
"Priority :$priority \n"
}
if (QuerySnapshot.size() > 0) {
text += "---------------\n\n"
textView_data.append(text)
lastResult = QuerySnapshot.documents[QuerySnapshot.size() - 1]
}
}
}
希望得到一些幫助
謝謝
測試所需代碼: JavaActivity KotlinActivity Note Model和activity_main.xml
好吧,在遇到同樣的問題之后。 它是通過使用非空強制轉換 (!!) 解決的。 例如:
val query = transactionsCollection
.orderBy(Field.CREATED, Query.Direction.DESCENDING)
.startAfter(lastDocument!!)
.limit(PAGE_SIZE)
在您的情況下,它將是:
query = notebookRef.orderBy("priority")
.startAfter(lastResult!!)
.limit(3);
它正在 Kotlin 上工作。
你必須在 Kotlin 中以不同的方式實現它。 而不是保留對最后一個結果的引用,您必須保留對使用 lastResult 創建的查詢的引用。
刪除全局 lastResult 並將其替換為:
private var query: Query? = null
然后像這樣實現 LoadNotes:
private fun loadNotes() {
if(query == null){
query = notebookRef.orderBy("priority").limit(3)
}
query!!.get().addOnSuccessListener { QuerySnapshot ->
var text = ""
for (queryDocumentSnapshot in QuerySnapshot) {
val note: Note = queryDocumentSnapshot.toObject(Note::class.java)
note.docID = queryDocumentSnapshot.id
val title = note.title
val description = note.description
val priority = note.priority
text += "ID: ${note.docID}\nTitle: $title \nDescription: $description"+
"\nPriority: $priority\n"
}
if (QuerySnapshot.size() > 0) {
text += "---------------\n\n"
textView_data.append(text)
val lastResult = QuerySnapshot.documents[QuerySnapshot.size() - 1]
query = notebookRef.orderBy("priority").startAfter(lastResult).limit(3)
}
}
}
最后兩行是使它工作的原因。 老實說,我仍然不明白為什么它不像在 Java 中那樣工作,但至少這個實現是有效的。 firebase 文檔也建議像這樣實現它。
您的代碼按預期工作。 您在 Kotlin 中所做的事情與 Java 沒有區別。 在這兩種情況下,您都將限制指定為 3。
如果有任何問題,這將取決於您的邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.