RxJava2 action not executed if user leaves screen
Asked Answered
T

1

7

I do a remove action through RxJava2 that causes a refresh on my local cache like this:

override fun removeExperience(experienceId: String, placeId: String): Completable {
    return from(placesApi.deleteExperience(experienceId, placeId))
        .andThen(from(refreshPlace(placeId))
            .flatMapCompletable { Completable.complete() }
        )
    }

so whenever the remove action is done (Completable is complete), a refresh is triggered. The problem is, sometimes this remove action takes long enough for users to just leave the screen, and then the andThen action is never executed cause there is no subscribers anymore, and thus the information on the screen is not up to date anymore.

Is there a way to enforce this action to take place?

Topper answered 18/7, 2018 at 16:3 Comment(3)
can you try moving the operation to be performed by a component that is bound to the application's lifecycle (rather than the screen)?Molybdenum
like a service? what other component would work?Topper
it really depends on your app's architecture. it could be a service. it could be the Application instance (or some other member that it delegates to). the key point is you wouldn't be dependent on the viability of the Activity/Fragment instance. if you're able to share more about the layout/design i might be able to offer more concrete recommendations.Molybdenum
P
0

Does this logic continue working when user open the same screen again? If so, then you only need to finish subscription from(placesApi.deleteExperience(experienceId, placeId)) on lifecycle events. The easiest way is to add the whole subscription removeExperience() to Disposable or CompositeDisposable and then trigger its .dispose() or .clear() on view stop or destroy events.

  • .dispose() - doesn't allow to use the same subscription stored.
  • .clear() - allows re-subscription without creating the new subscription instance
Pandiculation answered 27/6, 2019 at 7:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.