VictorAlbertos / RxCache

Reactive caching library for Android and Java
Apache License 2.0
2.38k stars 194 forks source link

RxCache returning only first value of an Observable stream #136

Open apexkid opened 5 years ago

apexkid commented 5 years ago

I was able to make RxCache with retrofit work by creating a provider which matches the return type of the retrofit api interface. However, if i first transform the API output to another Observable and try to cache on it, the cache only returns the first object in the stream.

I have a function in my repository like this:

 public Observable<FeaturedUserRecord> getFeaturedUsersFromService(boolean forceAPIReload) {
        Log.v(TAG, "Trying to getFeaturedUsersFromService");

        final Observable<FeaturedUsersResponse> output = apiService.getFeaturedUsers();
        final Observable<FeaturedUserRecord> result = output.flatMapIterable(listRest -> listRest.getData())
                .map(apiEntity -> transformer.transform(apiEntity));

        return result;
    }

I want to cache on the return value of the function instead on the return value of the apiService. Therefore, i created a provider like:

    @ProviderKey("featured-users-cached2")
    @LifeCache(duration = 5, timeUnit = TimeUnit.MINUTES)
    Observable<FeaturedUserRecord> getFeaturedUsersCached2(
            Observable<FeaturedUserRecord> functionOutput, EvictProvider evictProvider
    );

The first time data is loaded, it sends all the records received from the api. However, the cache only returns the first value of the observable stream on subsequent refreshes.

apexkid commented 5 years ago

Is it the case here that RxCache can cache Observable<List<MyObj>> but it cannot work with Observable<MyObj>?