Scarlet
Scarlet copied to clipboard
Retry policy doesn't take the AndroidLifecycle into account
I have a Scarlet instance inside a Android LifecycleService which has been started in foreground, in order for the service to not be destroyed when the application is not in foreground anymore. I've set the Scarlet lifecycle owner as the LifecycleService in which it resides.
I'm trying to start the Scarlet instance, but it fails (404 not found), hence the BackoffStrategy kicks in, which works as expected. However, if I kill the service (its onDestroy is called), the Scarlet instance continues to retry connecting to the server. I expected it to die along with the service.
Unfortunately, no WebSocket event is being called, thus I don't have access to the WebSocket object and I cannot close it when the service gets destroyed:
disposables += webSocketService.observeOnConnectionEvent().subscribe { when (it) { is WebSocket.Event.OnConnectionOpened<*> -> { Log.i(TAG, "Web socket opened") webSocket = it.webSocket as WebSocket } is WebSocket.Event.OnConnectionClosing -> Log.i(TAG, "Web socket closing") is WebSocket.Event.OnConnectionClosed -> Log.i(TAG, "Web socket closed") is WebSocket.Event.OnConnectionFailed -> Log.e(TAG, "Web socket failed", it.throwable) is WebSocket.Event.OnMessageReceived -> Log.i(TAG, "Web socket message received $it.message") } }
Did you use the scarlet-lifecycle-android plugin? This plugin should react to lifecycle changes and stop the scarlet instance from retrying
It can be passed into the Scarlet builder like this:
val scarlet = Scarlet.Builder()
// ...
.lifecycle(AndroidLifecycle.ofLifecycleOwner(yourLifecycleService))
.build()
Yes, that's what I've already done, I've used the service's lifecycle for Scarlet. But it doesn't appear to work. Or am I missing something?
For the record, the method used is:
Scarlet.Builder().lifecycle(AndroidLifecycle.ofLifecycleOwnerForeground(context as App, lifecycleOwner))
It doesn't work, the Scarlet service is not started when the service is running.
@bogdanzurac I found out what the issue was and fixed it in 0.2.4-alpha1 by introducing a new method: AndroidLifecycle#ofLifecycleServiceStarted.
The existing method, ofLifecycleOwnerForeground, uses onResume and onPause events to describe the scope when a view/fragment/activity is in foreground.
However, it is not the correct way to describe when a LifecycleService is running because a LifecycleService never triggers onResume and onPause. We need to use onStart and onStop instead.
@bogdanzurac I also added a demo that uses LifecycleService