Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Slake07 opened this issue Feb 6, 2019 · 1 comment

Comments

@Slake07
Copy link

Slake07 commented Feb 6, 2019

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
Copy link
Owner

amrro commented Feb 9, 2019

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants