#3543·retrofit

Blocking UI thread on first initialization

Author: ArcherEmiya05Created Apr 14, 2021Updated Nov 4, 2025

I am getting a lot of hiccups Choreographer: Skipped 59 frames! The application may be doing too much work on its main thread. during first initialization of Retrofit which is noticeable when switching between fragments using navigation drawer. It will be fixed if I add delay on making request but I want to know if there is any neat solution on this? I tried to use Kotshi as it was stated on the other issue that it performs well than Moshi but no luck.

Home Fragment extending Parent Fragment

disposable = initRetrofit.getAssetItems(
            Singleton.assetField,
            limit
        )
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                { result ->

                    swipeRefreshLayout.isRefreshing = false

                    swipeRefreshLayout.isEnabled = false // Swipe gesture no longer needed

                    adapter.submitList(result.data)

                    logTxt.text = null

                },
                { error ->
                    Log.wtf("WTF", "${error.message}")
                    swipeRefreshLayout.isRefreshing = false
                    if (adapter.currentList.isEmpty() || (error is HttpException && error.code() == HttpURLConnection.HTTP_GATEWAY_TIMEOUT)){
                        adapter.submitList(mutableListOf()) // Pass an empty list if no cache
                        logTxt.setText(R.string.swipe_to_refresh)
                    }
                }
            )

Parent Fragment

protected val initRetrofit by lazy {
        EndpointServices.create(url, requireContext())
    }

EndpointServices class with companion object function create(string,context)

val httpClient = OkHttpClient.Builder()
                .cache(cache)
                .addInterceptor(interceptor)
                .callTimeout(10, TimeUnit.SECONDS)
                .connectTimeout(10, TimeUnit.SECONDS)
                .addNetworkInterceptor(interceptor())
                .addInterceptor(onlineOfflineHandling())
                .build()

            val retrofit = Retrofit.Builder()
                .addCallAdapterFactory(
                    RxJava2CallAdapterFactory.create()
                )
                .addConverterFactory(
                    MoshiConverterFactory.create()
                )
                .client(httpClient)
                .baseUrl(baseUrl)
                .build()