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();

###Custom Memory Caching Policy By default stores will cache up to 100 items for 24 hours using an expireAfterAccess policy. You can pass in your own Cache instance to override the duration and type of caching policy. The below example will cache 1 item forever allowing you to keep the memory footprint of the store lower

ParsingStoreBuilder.<BufferedSource,RedditData>builder()
                .fetcher(this::fetcher)
                .parser(GsonParserFactory.createSourceParser(provideGson(),RedditData.class))
                .memory(CacheBuilder.newBuilder()
                        .maximumSize(1)
                        .expireAfterWrite(Long.MAX_VALUE, TimeUnit.SECONDS)
                        .build())
                .open();

###Subclassing Stores There are times when you'd like to build a store with a few additional helper methods. Subclassing stores is your best bet. Based on what you pass into the constructor will denote how store is configured

public class SampleStore extends RealStore<String> {
     public SampleParsingStore(Fetcher<String> fetcher, Persister<String> persister, Parser<String, String> parser) {
        super(fetcher, persister, parser);
    }

    public SampleStore(Fetcher<String> fetcher, Persister<String> persister) {
        super(fetcher, persister);
    }
    public SampleStore(Fetcher<String> fetcher) {
        super(fetcher);
    }
}

###Disk Caching with SourcePersister If you want to enable disk caching the easiest way is to use the Source Persister by adding
compile 'com.nytimes.android:filesystem:VERSION' to your build.gradle

A source persister can persist something that is a BufferedSource. Most easily you can get a BufferedSource from Okhttp Response or Retrofit ResponseBody Once you have a fetcher that returns a BufferedSource you can make a Source Persister Persister bufferedSourcePersister = SourcePersisterFactory.create(getApplicationContext().getCacheDir());

Ideally the SourcePersister would be shared among many stores

StoreBuilder.<BufferedSource>builder()
                .fetcher(this::fetcher)
                .persister(persister)
                .open();

Most use cases will want to parser the data into a pojo rather than returning a BufferedSource middleware parsers work perfectly with the SourcePersister

ParsingStoreBuilder.<BufferedSource,RedditData>builder()
                .fetcher(this::fetcher)
                .persister(persister)
                .parser(MoshiParserFactory.createSourceParser(new Moshi.Builder().build(),RedditData.class))
                .open();
ParsingStoreBuilder.<BufferedSource,RedditData>builder()
                .fetcher(this::fetcher)
                .persister(persister)
                .parser(JacksonParserFactory.createSourceParser(new ObjectMapper(),RedditData.class))
                .open();
ParsingStoreBuilder.<BufferedSource,RedditData>builder()
                .fetcher(this::fetcher)
                .persister(persister)
                .parser(GsonParserFactory.createSourceParser(new Gson(),RedditData.class))
                .open();
Clone this wiki locally