kotlinparse-platformparse-android-sdk

Kotlin/Parse : com.parse.ParseObject cannot be cast to


I'm trying to do a Parse request but I got this error :

com.parse.ParseObject cannot be cast to com.mysapp.mys.common.model.Lesson

but it doesn't enter inside the subscribe after the return of the request. The answer of the server is good because I have the log of it on real-time.

I believe I have a problem when I receive the answer and when I cast implicitly with the lambda. But I don't know how to achieve it in another way.

fun getLessons(coach: Boolean, lessonType: LessonType?, date: Date?, position: LatLng, distance: Double): Single<List<Lesson>> {
        val params = hashMapOf<String, Any?>()
        if (lessonType == null){
            params["lessonType"] = ""
        }
        else{

        }
        params["lessonType"] = 12

        params["date"] = date
        params["geoPoint"] = ParseGeoPoint(44.557514, -0.86099)
        params["within"] = 4935.772
        Log.e("params : ", "lessonType: " + params.get("lessonType") + "| date : " + params.get("date").toString()
                + " | geoPoint: " + params.get("geoPoint") + " | within: "+ params.get("within"))
        return ParseObservable.callFunction<List<Lesson>>("getSessions", params).singleOrError()
    }

how I treat it :

disposables.add(repository.getLessons(false, lesson, dateSelected, position, distance.toDouble())
                .observeOn(Schedulers.io())
                .subscribe({ lessons ->
                    Log.e("LESSONS", lessons.toString())
                    removeCanceledLessonsAndEmit(lessons)
                }, Throwable::printStackTrace)
        )

I don't know how Parse is working so that's why I have copied on this

request who already exists in the project :

fun getAverageRating(coachId: String): Single<HashMap<String, Int>> {
        val params = hashMapOf<String, String>()
        params["coachId"] = coachId
        return ParseObservable.callFunction<HashMap<String, Int>>("getAverageRating", params).singleOrError()
    }

how it's treated :

disposables.add(repository.getAverageRating(coachId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ rating ->
                    if (rating > 0) {
                        ratingBar.visibility = View.VISIBLE
                        ratingBar.rating = rating
                    } else
                        ratingBar.visibility = View.GONE

                }, Throwable::printStackTrace)
        )

If you need any details feel free to ask me.


Solution

  • The following method getSessions is returning a ParseObject instead of returning a Lesson type object. Try to check the type and return appropriate model from that.

    Change getLessons return type to this

       ParseObservable.callFunction<List<Any>>("getSessions", params).singleOrError()
    

    and treat like this to check return type.

    disposables.add(repository.getLessons(false, lesson, dateSelected, position, distance.toDouble())
                    .observeOn(Schedulers.io())
                    .subscribe({ lessons ->
                        Log.e("LESSONS", lessons.toString())
                        //removeCanceledLessonsAndEmit(lessons)
                    }, Throwable::printStackTrace)
            )
    

    To get lessons in com.mysapp.mys.common.model.Lesson POJO you can do any of the followings

    1. Annotate Lessons class with @ParseClassName("Lesson") See this answer for details

    2. Cast parse object to JSON and use gson to convert to Lesson POJO

    Finally, don't forget to do the necessary changes to this line ParseObservable.callFunction>("getSessions", params).singleOrError()