Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce LoadingStrategy.CacheOnly #48

Closed
wants to merge 1 commit into from
Closed

Conversation

PavloRatushnyi
Copy link
Contributor

@PavloRatushnyi PavloRatushnyi commented May 15, 2024

It will allow to take data only from cache, without making network request

@yatsinar
Copy link
Member

Can you pls describe the use case for this loading mode? Specifically why LazyReload can't be used here - it wouldn't reload the data if storage cache is present. Once the data is in storage, it would effectively work as CacheOnly, and the state with complete absence of storage and mem cache is rather an edge case

@PavloRatushnyi
Copy link
Contributor Author

In my situation the use case is that I take the data from one DoD, and based on this data I decide if I need to fetch data from another DoD.
Something like:

featureEnabledDodFlow.observe(params = Unit, loadingStrategy = LoadingStrategy.ForceReload)
    .extractContent()
    .flatMapLatest { featureEnabled: Boolean ->
        if (featureEnabled) {
            configDodFlow.observe(params = Unit, loadingStrategy = LoadingStrategy.ForceReload)
        } else {
            flowOf(null)
        }
    }

The thing is that when featureEnabledDodFlow returns data from cache, I do not know if it is up-to-date, so in other words I do not know if the feature is really enabled. And when it returns Data(content = true, loading = true), I call configDodFlow.observe(LoadingStrategy.ForceReload). The network request made by configDodFlow can fail, because in reality featureEnabled can be false.
So I do not want to make network request from configDodFlow when featureEnabledDodFlow returns Data with loading = true. In this case I want configDodFlow to return data from cache only.
So I want to have something like this:

featureEnabledDodFlow.observe(params = Unit, loadingStrategy = LoadingStrategy.ForceReload)
    .flatMapLatest { featureEnabledData: Boolean ->
        if (featureEnabledData.content == true) {
            configDodFlow.observe(
                params = Unit,
                loadingStrategy = if (featureEnabledData.loading) LoadingStrategy.CacheOnly else LoadingStrategy.ForceReload
            )
        } else {
            flowOf(null)
        }
    }

LoadingStrategy.LazyReload suits my use case only if data for configDodFlow is in cache. If it is not in cache, then network request, which should not be made when featureEnabledDodFlow returns Data(content = true, enabled = true), will actually be made.

@yatsinar
Copy link
Member

yatsinar commented May 20, 2024

First of all flatMapping DODs in that manner is wrong: the subscriber will end up with a composed stream consisting of Observables produced by every emission of the featureEnabledDod.
You likely need switchMap operator here to keep only the last one subscribed.

The safest option is to switchMap the upstream that filters out emissions untilLoaded, i.e. it will switchMap exactly once. But that would yield a delay in the subscriber receiving any values though.

But I think the root problem here is the underlying need for one DOD to depend on another and keep emissions following DOD contract for the subscriber.
The better way to do that is to subscribe to the upstream DOD in the fromNetwork stream of configDod, wait until value is loaded and switch map to either BE call or some stub value. In that way DOD will handle all intermediate emissions: it will return (loading, null), (loading, cachedConfig) and then (!loading, newConfig) or (!loading, noConfig) in case feature toggle was toggled off in the upstream.

@PavloRatushnyi
Copy link
Contributor Author

First of all flatMapping DODs in that manner is wrong: the subscriber will end up with a composed stream consisting of Observables produced by every emission of the featureEnabledDod.
You likely need switchMap operator here to keep only the last one subscribed.

You are talking here about RxJava, but I was using Flow in the example. flatMapLatest for Flow is analog of switchMap for RxJava.

I see your point. DataObservableDelegate is designed to have fromNetwork as mandatory, and caches as optional. And LoadingStrategy controls when data should be reloaded, but not how it should be emitted. I decline this PR then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants