Я получаю данные из базы данных пожарного магазина, используя следующий код:
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 at 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 $osByOrder (BrowseFragment.kt: 44) в com.firestoredemo.ui.fragment.BrowseFragment $ setListener $ 2.onLoadMore (BrowseFragment.kt: 153) в com.firestoredemo.adapter.BrowseResultAdapter $ 1.onScrolled (BrowseResultAdapter.kt 57):*