Scarlet
Scarlet copied to clipboard
Lifecycle not shutting down scarlet (Activity)
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...
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
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 :/
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 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'
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...
@charlieAndroidDev Have you managed to solve the issue?
This worked for me
scarletInstance = Scarlet.Builder() .webSocketFactory(httpClient.newWebSocketFactory("WEB_SOCKET_URL")) .addMessageAdapterFactory(JacksonMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapterFactory()) .lifecycle(AndroidLifecycle.ofLifecycleOwnerForeground(application,this)) .build()
@ilyaklyukin how did you solve it?