So i am kind of experimenting with an app and i am making an okhttp request. I found out that this request task is made on its own thread, which leads to an asynchronous behaviour.
Down below you see my function which gets the data from the API, and the onCreateView function, where i call fetchData().
@Throws(IOException::class)
fun fetchData() {
val client = OkHttpClient()
val request: Request = Request.Builder()
.url("https://www.themealdb.com/api/json/v1/1/search.php?f=a")
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
call.cancel()
}
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
val responseString = U.formatJson(response.body?.string())
val meals = Gson().fromJson(responseString, Meals::class.java)
Log.d(TAG, "First")
}
})
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
fetchData()
Log.d(TAG, "second")
return inflater.inflate(R.layout.fragment_home, container, false)
}
So i put these logs here to see, which Log gets called first, and since fetchData() is asynchronous, it is obvious that Log.d(TAG, "second"), which is in onCreateView(), gets called first. So if i try to use the data that is getting requested down below, i get null because the request has not yet finished. And that is my problem. I need to use the requested data after calling the function.
Is there any solution where i can do fetchData() somehow synchronous, but without blocking the main Thread?