我正在使用以下代码从Firestore数据库中获取数据:
ImageDataModel.kt
class ImageDataModel @Inject constructor(private val firebaseFirestore: FirebaseFirestore) : ViewModel() {
var isLoading = MutableLiveData<Boolean>()
var isPagerLoading = MutableLiveData<Boolean>()
var apiError = MutableLiveData<String>()
var imgResponse = MutableLiveData<QuerySnapshot>()
fun getPhotosByOrder(orderBy: String){
isLoading.value = true
var query: Query
query = firebaseFirestore.collection(DBConstant.PHOTO.tableNm)
.orderBy(orderBy)
.limit(12)
query.get()
.addOnSuccessListener { documentSnapshots ->
// Get the last visible document
if(documentSnapshots != null){
isLoading.value = false
imgResponse.value = documentSnapshots
}else{
isLoading.value = false
apiError.value = Constant.SERVER_CONNECTION_ERROR
}
}
.addOnFailureListener {
isLoading.value = false
apiError.value = it.message
}
}
fun getPhotosByOrderPagination(orderBy: String, lastVisible: DocumentSnapshot?){
isPagerLoading.value = true
var query: Query
query = firebaseFirestore.collection(DBConstant.PHOTO.tableNm)
.orderBy(orderBy)
.startAfter(lastVisible)
.limit(6)
query.get()
.addOnSuccessListener { documentSnapshots ->
// Get the last visible document
if(documentSnapshots != null){
isPagerLoading.value = false
imgResponse.value = documentSnapshots
}else{
isPagerLoading.value = false
apiError.value = Constant.SERVER_CONNECTION_ERROR
}
}
.addOnFailureListener {
isPagerLoading.value = false
apiError.value = it.message
}
}
}
活动分类:
private var lastVisible: DocumentSnapshot?= null
private fun bindObservers(){
imageDataModel.isLoading.observe(this, androidx.lifecycle.Observer {
if(it){
llPbLoading.visibility = View.VISIBLE
rvListFragBrowse.visibility = View.GONE
}else{
llPbLoading.visibility = View.GONE
rvListFragBrowse.visibility = View.VISIBLE
}
})
imageDataModel.apiError.observe(this, androidx.lifecycle.Observer {
toast(it, activity!!)
})
imageDataModel.imgResponse.observe(this, androidx.lifecycle.Observer {
photosList.clear()
if(it.size() > 0){
lastVisible = it.documents[it.size() - 1]
Log.e("BrowseFrag","Last Item Name: "+lastVisible!!)
for(item in it){
var photoData = item.toObject(PhotosData::class.java)
photoData.id = item.id
photosList.add(photoData)
mAdapterBrowse.notifyDataSetChanged()
}
}
})
}
private fun getPhotosByOrder(orderBy: String){
if(lastVisible == null){
imageDataModel.getPhotosByOrder(orderBy)
}else{
imageDataModel.getPhotosByOrderPagination(orderBy, lastVisible)
}
}
我收到以下错误日志:
java.lang.IllegalArgumentException:无效的数据。不支持的类型:com.google.firebase.firestore.QueryDocumentSnapshot com.google.firebase.firestore.core.UserData $ ParseContext.createError(com.google.firebase:firebase-firestore @@ 17.1.5:293) 在com.google.firebase.firestore.UserDataConverter.parseScalarValue(com.google.firebase:firebase-firestore @@ 17.1.5:405) 在com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore @@ 17.1.5:254) 在com.google.firebase.firestore.UserDataConverter.parseQueryValue(com.google.firebase:firebase-firestore @@ 17.1.5:186) 在com.google.firebase.firestore.Query.boundFromFields(com.google.firebase:firebase-firestore @@ 17.1.5:669) 在com.google.firebase.firestore.Query.startAfter(com.google.firebase:firebase-firestore @@ 17.1.5:517) 在com.firestoredemo.viewmodel.ImageDataModel.getPhotosByOrderPagination(ImageDataModel.kt:64) 在com.firestoredemo.ui.fragment.BrowseFragment.getPhotosByOrder(BrowseFragment.kt:228) 在com.firestoredemo.ui.fragment.BrowseFragment.access $ getPhotosByOrder(BrowseFragment.kt:44) 在com.firestoredemo.ui.fragment.BrowseFragment $ setListener $ 2.onLoadMore(BrowseFragment.kt:153) 在com.firestoredemo.adapter.BrowseResultAdapter $ 1.onScrolled(BrowseResultAdapter.kt:57)