Tinder / Scarlet

A Retrofit inspired WebSocket client for Kotlin, Java, and Android
Other
3.24k stars 238 forks source link

Lifecycle not shutting down scarlet (Activity) #118

Open charlie-niekirk opened 4 years ago

charlie-niekirk commented 4 years ago

I'm using: AndroidLifecycle.ofLifecycleOwnerForeground(application, activity, THROTTLE_TIMEOUT) as my lifecycle and passing this into scarlet via ::lifecycle() builder method. However, when my activity is paused/stopped scarlet continues to attempt reconnection. Is there lterally no way to shutdown Scarlet manually? I've tried with a LifecyleRegistry and setting the state manually, which also does nothing...

mitsinsar commented 4 years ago

Hi Charlie, I had the same issue. You can try this library until we get 0.2 release. They fixed this issue on 0.2 release. https://github.com/hantrungkien/tinder-scarlet-lifecycle-android

charlie-niekirk commented 4 years ago

Thanks @mitsinsar, I thought you only had the issue with services? Also you version requires API 21, and I'm having to work with 19 :/

mitsinsar commented 4 years ago

I had issue with lifecycle. If api sends something when app is on background, it crashes. It was because of lifecycle. Scarlet wasn't really lifecycle aware. They tracked something different. I forgot the details. And as far as I know, you have to use over 21 to use lifecycle awareness. But I might be wrong.

ilyaklyukin commented 4 years ago

@mitsinsar Could you, pls, describe, how I can use that lib?

For now I'm using scarlet with Snapshot version:

rep: maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
version: scarlet           : '0.2.5-SNAPSHOT'

implementation "com.tinder.scarlet:scarlet:${versions.scarlet}"

    implementation "com.tinder.scarlet:protocol-websocket-okhttp:${versions.scarlet}"
    implementation "com.tinder.scarlet:message-adapter-gson:${versions.scarlet}"
    implementation "com.tinder.scarlet:stream-adapter-coroutines:${versions.scarlet}"
    implementation "com.tinder.scarlet:lifecycle-android:${versions.scarlet}"
    implementation "com.squareup.okhttp3:okhttp:${versions.okhttp3}"
    implementation "com.squareup.okhttp3:logging-interceptor:${versions.okhttp3}"
    implementation "com.google.code.gson:gson:${googleVersions.gson}"
//    implementation 'com.github.hantrungkien:tinder-scarlet-lifecycle-android:1.0.0'

but implementation 'com.github.hantrungkien:tinder-scarlet-lifecycle-android:1.0.0' has a conflict, it uses: implementation 'com.github.tinder.scarlet:scarlet:0.2.4'

ilyaklyukin commented 4 years ago

Imported as a module, updated dependencies, but still having issue: using lifecycle injection (to be able to manage a connection not only with activity lifecycle),

    @Provides
    @Singleton
    @JvmStatic
    fun provideScarletLifecycleRegistry(): LifecycleRegistry =
        //LifecycleRegistry(ClientConfig.DISCONNECT_DELAY_MS)
        LifecycleRegistry(0L)

    @Provides
    @Singleton
    @JvmStatic
    fun provideScarlet(
        lifecycleRegistry: LifecycleRegistry,
        httpClient: OkHttpClient,
        @Named("apiBaseUrl") baseUrl: String
    ): Scarlet = Scarlet(
        OkHttpWebSocket(
            httpClient,
            OkHttpWebSocket.SimpleRequestFactory(
                { Request.Builder().url(baseUrl).build() },
                { ShutdownReason.GRACEFUL }
            )),
        Scarlet.Configuration(
            lifecycle = lifecycleRegistry,
         //   backoffStrategy = LinearBackoffStrategy(ClientConfig.RECONNECT_INTERVAL_MS),
            messageAdapterFactories = listOf(GsonMessageAdapter.Factory()),
            streamAdapterFactories = listOf(CoroutinesStreamAdapterFactory()),
            debug = BuildConfig.DEBUG
        )
    )

and setting it manually in Activity:

override fun onPause() { //init closing ws connection lifecycleRegistry.onNext(LifecycleState.Stopped) ... }

but in case of no connection , when app is in background - connection still tries to reconnect. Need to stop any tries of reconnection in background...

ilyaklyukin commented 4 years ago

@charlieAndroidDev Have you managed to solve the issue?

chathudan commented 4 years ago

This worked for me

scarletInstance = Scarlet.Builder() .webSocketFactory(httpClient.newWebSocketFactory("WEB_SOCKET_URL")) .addMessageAdapterFactory(JacksonMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapterFactory()) .lifecycle(AndroidLifecycle.ofLifecycleOwnerForeground(application,this)) .build()

hbb20 commented 3 years ago

@ilyaklyukin how did you solve it?