Scarlet icon indicating copy to clipboard operation
Scarlet copied to clipboard

Lifecycle not shutting down scarlet (Activity)

Open charlie-niekirk opened this issue 5 years ago • 8 comments

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...

charlie-niekirk avatar Nov 06 '19 12:11 charlie-niekirk

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

mitsinsar avatar Nov 06 '19 12:11 mitsinsar

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 :/

charlie-niekirk avatar Nov 06 '19 17:11 charlie-niekirk

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.

mitsinsar avatar Nov 07 '19 08:11 mitsinsar

@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 avatar Nov 19 '19 13:11 ilyaklyukin

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 avatar Nov 19 '19 14:11 ilyaklyukin

@charlieAndroidDev Have you managed to solve the issue?

ilyaklyukin avatar Nov 19 '19 14:11 ilyaklyukin

This worked for me

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

chathudan avatar Mar 17 '20 08:03 chathudan

@ilyaklyukin how did you solve it?

hbb20 avatar Aug 04 '21 19:08 hbb20