1

I have a working application, but I would like to improve one point. The method ("private fun save"), which is responsible for saving the information I need, I would like to make asynchronous.

But the problem is that when I change it to - "private suspend fun save", I have to make suspend and override fun intercept method. But since it is override, I get an error:

Conflicting overloads: public open suspend fun intercept(chain: Interceptor.Chain): Response defined in com.pocketscout.network.PocketScoutInterceptor, public abstract fun intercept(chain: Interceptor.Chain) : Response defined in okhttp3.Interceptor.

Is this problem somehow solved?

    class PocketScoutInterceptor(
    private val appContainer: PocketScoutContainer,
) : okhttp3.Interceptor {

    @Throws(IOException::class)
    override fun intercept(chain: okhttp3.Interceptor.Chain): okhttp3.Response {
        val packet = buildPacket(timestamp, duration, request, response, description)

        save(packet)

        return response ?: okhttp3.Response.Builder().build()
    }
    
 
Paul
  • 53
  • 3
  • 21
  • Does this answer your question? [call a suspend function inside a normal function](https://stackoverflow.com/questions/56409970/call-a-suspend-function-inside-a-normal-function) – Sky May 18 '22 at 10:00
  • @Sky I haven't seen this answer before, thanks! Only I do not fully understand how I can apply runBlocking in my code – Paul May 18 '22 at 10:13

1 Answers1

0

At this moment OkHttp3 doesn't support suspend feature for interceptors. You can wrap save method

private suspend fun save(packet: Packet) { // Make suspend
   ...
}

runBlocking { save(packet) } // Call inside intercept

or all inside it:

private fun save(packet: Packet) = runBlocking {
   ...
}

Use this code at your own risk.

Sky
  • 640
  • 5
  • 12