Home > other >  How do I get the value out of the onResponse function of retrofit to return it in the outer class
How do I get the value out of the onResponse function of retrofit to return it in the outer class

Time:07-12

The below is my code where im trying to return false whenever the body is not empty, but the variable blocker is not overwritten when the body has content in it. The code itself is working but it seems to not get the Code out of the onResponse method.

private fun checkTime(date : String, time : String): Boolean {
        val blocker = true
        val checkapt = ReservationBySalon(
            listOf(CheckApt(
                hairdresser_key = "asru6sxqrifl",
                date = date,
                status = "aktiv",
                time_from = time)
        ))
        val retrofitData = DbApi.retrofitService.validateAppointment(checkapt)
        retrofitData.enqueue(object : Callback<ReservationsList?> {

            override fun onResponse(
                call: Call<ReservationsList?>,
                response: Response<ReservationsList?>
            ) {
                if(response.body()?.paging.toString() > 0.toString()){
                    blocker = false

                }else{
                }
            }

            override fun onFailure(call: Call<ReservationsList?>, t: Throwable) {
                Log.e("Reserve", "onFailure: "   t.message)
            }
        })
        return blocker
    }

Answers are much appreciated

CodePudding user response:

Try to change val to var as the value can change

 private fun checkTime(date : String, time : String): Boolean {

    var blocker = true
    val checkapt = ReservationBySalon(
        listOf(CheckApt(
            hairdresser_key = "asru6sxqrifl",
            date = date,
            status = "aktiv",
            time_from = time)
        ))
    val retrofitData = DbApi.retrofitService.validateAppointment(checkapt)
    retrofitData.enqueue(object : Callback<ReservationsList?> {

        override fun onResponse(
            call: Call<ReservationsList?>,
            response: Response<ReservationsList?>
        ) {
            if(response.body()?.paging.toString() > 0.toString()){
                blocker = false

            }else{
            }
        }

        override fun onFailure(call: Call<ReservationsList?>, t: Throwable) {
            Log.e("Reserve", "onFailure: "   t.message)
        }
    })
    return blocker
}

CodePudding user response:

You are running asynchronous code and then return blocker before the response is received.

checkTime must not return a Boolean. If you want to stick with callbacks instead of Flows, you can specify a callback method which is called in checkTime:

private fun checkTime(
    date : String,
    time : String,
    onTimeChecked: (Boolean?) -> Unit): Boolean {
    ...
    retrofitData.enqueue(object : Callback<ReservationsList?> {

        override fun onResponse(
            call: Call<ReservationsList?>,
            response: Response<ReservationsList?>
        ) {
            if(response.body()?.paging.toString() > 0.toString()){
                onTimeChecked(false)
            }else{
            }
        }

        override fun onFailure(call: Call<ReservationsList?>, t: Throwable) {
            Log.e("Reserve", "onFailure: "   t.message)
        }
    })
    return blocker
}
  • Related