amrro / firestore-android-arch-components

Firestore sample with Android architecture component.
Apache License 2.0
107 stars 31 forks source link

Invalid data. Unsupported type: com.google.firebase.firestore.QueryDocumentSnapshot #12

Open Slake07 opened 5 years ago

Slake07 commented 5 years ago

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
        }

}

}

Activity class:

  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)
    }
}

I am getting below error log: java.lang.IllegalArgumentException: Invalid data. Unsupported type: com.google.firebase.firestore.QueryDocumentSnapshot at com.google.firebase.firestore.core.UserData$ParseContext.createError(com.google.firebase:firebase-firestore@@17.1.5:293) at com.google.firebase.firestore.UserDataConverter.parseScalarValue(com.google.firebase:firebase-firestore@@17.1.5:405) at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@17.1.5:254) at com.google.firebase.firestore.UserDataConverter.parseQueryValue(com.google.firebase:firebase-firestore@@17.1.5:186) at com.google.firebase.firestore.Query.boundFromFields(com.google.firebase:firebase-firestore@@17.1.5:669) at com.google.firebase.firestore.Query.startAfter(com.google.firebase:firebase-firestore@@17.1.5:517) at com.firestoredemo.viewmodel.ImageDataModel.getPhotosByOrderPagination(ImageDataModel.kt:64) at com.firestoredemo.ui.fragment.BrowseFragment.getPhotosByOrder(BrowseFragment.kt:228) at com.firestoredemo.ui.fragment.BrowseFragment.access$getPhotosByOrder(BrowseFragment.kt:44) at com.firestoredemo.ui.fragment.BrowseFragment$setListener$2.onLoadMore(BrowseFragment.kt:153) at com.firestoredemo.adapter.BrowseResultAdapter$1.onScrolled(BrowseResultAdapter.kt:57)

amrro commented 5 years ago

Hello @Slake07, It seems that you data model is different than the one in the database. I cannot debug the code like this. But mostly the problem is here:

 query = firebaseFirestore.collection(DBConstant.PHOTO.tableNm)
        .orderBy(orderBy)
        .startAfter(lastVisible)
        .limit(6)