Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Recipes

Mike Nakhimovich edited this page Jan 19, 2017 · 20 revisions

Welcome to the Store Recipes!

Cached then fresh data

First call get and concat the result with fetch. Next add a distinct operator which will only allow unique emissions. If cache and network return same value or if the first get call actually needs to go to network, only 1 item will be emitted.

store.get(barCode)
                .concatWith(store.fetch(barCode))
                .distinct()
                .subscribe()

MultiParser

Sometimes you want to use a middleware parser to go from json to pojo and then use another parser to unwrap your data.

       Parser<BufferedSource, RedditData> sourceParser = GsonParserFactory.createSourceParser(provideGson(), RedditData.class);
       Parser<RedditData, Data> envelopeParser = redditData -> redditData.data();

       ParsingStoreBuilder.<BufferedSource,RedditData>builder()
               .fetcher(this::fetcher)
               .persister(persister)
               .parser(new MultiParser<>(Arrays.asList(sourceParser,envelopeParser)))
               .open();

####Top Level JSON Array In some cases you may need to parse a top level JSONArray, in which case you can provide a TypeToken.

Store<List<Article>> Store = ParsingStoreBuilder.<BufferedSource, List<Article>>builder()
                .nonObservableFetcher(this::getResponse)
                .parser(GsonParserFactory.createSourceParser(gson, new TypeToken<List<Article>>() {}))
                .open();
Clone this wiki locally