Store/README.md

328 lines
16 KiB
Markdown
Raw Normal View History

2017-05-23 22:15:13 +00:00
[![Build Status](https://travis-ci.org/NYTimes/Store.svg?branch=feature/rx2)](https://travis-ci.org/NYTimes/Store)
2017-01-12 12:59:36 +00:00
![Store](https://www.milkadeal.com/storage/128000/118240/68ccf50a75b5277678c52752077c19b5.jpg)
2017-01-04 19:17:53 +00:00
2017-02-13 18:42:14 +00:00
Store is an Android library for effortless, reactive data loading.
2017-01-04 19:17:53 +00:00
### The Problems:
+ Modern Android Apps need their data representations to be fluid and always available.
+ Users expect their UI experience to never be compromised (blocked) by new data loads. Whether an application is social, news, or business-to-business, users expect a seamless experience both online and offline.
+ International users expect minimal data downloads as many megabytes of downloaded data can quickly result in astronomical phone bills.
2017-01-04 19:17:53 +00:00
A Store is a class that simplifies fetching, parsing, storage, and retrieval of data in your application. A Store is similar to the Repository pattern [[https://msdn.microsoft.com/en-us/library/ff649690.aspx](https://msdn.microsoft.com/en-us/library/ff649690.aspx)] while exposing a Reactive API built with RxJava that adheres to a unidirectional data flow.
Store provides a level of abstraction between UI elements and data operations.
2017-01-04 19:17:53 +00:00
### Overview
A Store is responsible for managing a particular data request. When you create an implementation of a Store, you provide it with a `Fetcher`, a function that defines how data will be fetched over network. You can also define how your Store will cache data in-memory and on-disk, as well as how to parse it. Since Store returns your data as an Observable, threading is a breeze! Once a Store is built, it handles the logic around data flow, allowing your views to use the best data source and ensuring that the newest data is always available for later offline use. Stores can be customized to work with your own implementations or use our included middleware.
2017-01-04 19:17:53 +00:00
Store leverages RxJava and multiple request throttling to prevent excessive calls to the network and disk cache. By utilizing Store, you eliminate the possibility of flooding your network with the same request while adding two layers of caching (memory and disk).
2017-01-04 19:17:53 +00:00
### Fully Configured Store
Let's start by looking at what a fully configured Store looks like. We will then walk through simpler examples showing each piece:
2017-01-04 19:17:53 +00:00
```java
2017-02-10 17:17:28 +00:00
Store<ArticleAsset, Integer> articleStore = StoreBuilder.<Integer, BufferedSource, ArticleAsset>parsedWithKey()
.fetcher(articleId -> api.getArticleAsBufferedSource(articleId)) //OkHttp responseBody.source()
2017-02-10 17:37:37 +00:00
.persister(FileSystemPersister.create(FileSystemFactory.create(context.getFilesDir()),pathResolver))
2017-02-10 17:17:28 +00:00
.parser(GsonParserFactory.createSourceParser(gson, ArticleAsset.Article.class))
.open();
2017-01-04 19:17:53 +00:00
```
With the above setup you have:
2017-02-10 17:17:28 +00:00
+ In Memory Caching for rotation
+ Disk caching for when users are offline
+ Parsing through streaming API to limit memory consumption
2017-02-10 17:17:28 +00:00
+ Rich API to ask for data whether you want cached/new or a stream of future data updates.
2017-01-04 19:17:53 +00:00
And now for the details:
### Creating a Store
You create a Store using a builder. The only requirement is to include a `.Fetcher<ReturnType,KeyType>` that returns an Observable<ReturnType> and has a single method `fetch(key)`
2017-01-04 19:17:53 +00:00
``` java
Store<ArticleAsset, Integer> store = StoreBuilder.<>key()
2017-02-10 17:17:28 +00:00
.fetcher(articleId -> api.getArticle(articleId)) //OkHttp responseBody.source()
.open();
2017-01-04 19:17:53 +00:00
```
Stores use generic keys as identifiers for data. A key can be any value object that properly implements toString and equals and hashCode. When your Fetcher function is called, it will be passed a particular Key value. Similarly, the key will be used as a primary identifier within caches (Make sure to have a proper hashCode!!)
2017-01-04 19:17:53 +00:00
2017-02-10 17:17:28 +00:00
### Our Key implementation - Barcodes
For convenience we included our own key implementation called a BarCode. Barcode has two fields `String key and String type`
2017-01-04 19:17:53 +00:00
``` java
2017-01-05 18:59:48 +00:00
BarCode barcode = new BarCode("Article", "42");
2017-01-04 19:17:53 +00:00
```
2017-02-10 17:17:28 +00:00
When using a Barcode as your key, you can use a StoreBuilder convenience method
``` java
Store<ArticleAsset, Integer> store = StoreBuilder.<ArticleAsset>barcode()
.fetcher(articleBarcode -> api.getAsset(articleBarcode.getKey(),articleBarcode.getType()))
.open();
```
2017-01-04 19:17:53 +00:00
2017-02-10 17:17:28 +00:00
### Public Interface - Get, Fetch, Stream, GetRefreshing
2017-01-04 19:17:53 +00:00
```java
2017-01-05 18:59:48 +00:00
Observable<Article> article = store.get(barCode);
2017-01-04 19:17:53 +00:00
```
The first time you subscribe to `store.get(barCode)`, the response will be stored in an in-memory cache. All subsequent calls to `store.get(barCode)` with the same Key will retrieve the cached version of the data, minimizing unnecessary data calls. This prevents your app from fetching fresh data over the network (or from another external data source) in situations when doing so would unnecessarily waste bandwidth and battery. A great use case is any time your views are recreated after a rotation, they will be able to request the cached data from your Store. Having this data available can help you avoid the need to retain this in the view layer.
2017-01-04 19:17:53 +00:00
So far our Stores data flow looks like this:
![Simple Store Flow](https://github.com/nytm/Store/blob/master/Images/store-1.jpg)
By default, 100 items will be cached in memory for 24 hours. You may pass in your own instance of a Guava Cache to override the default policy.
2017-01-04 19:17:53 +00:00
### Busting through the cache
Alternatively you can call `store.fetch(barCode)` to get an Observable that skips the memory (and optional disk cache).
2017-01-04 19:17:53 +00:00
Fresh data call will look like: `store.fetch()`
2017-01-04 19:17:53 +00:00
![Simple Store Flow](https://github.com/nytm/Store/blob/master/Images/store-2.jpg)
In the New York Times app, overnight background updates use `fetch` to make sure that calls to `store.get()` will not have to hit the network during normal usage. Another good use case for `fetch` is when a user wants to pull to refresh.
2017-01-04 19:17:53 +00:00
Calls to both `fetch()` and `get()` emit one value and then call `onCompleted()` or throw an error.
2017-01-04 19:17:53 +00:00
### Stream
For real-time updates, you may also call `store.stream()` which returns an Observable that emits each time a new item is added to the Store. You can think of stream as an Event Bus-like feature that allows you to know when any new network hits happen for a particular Store. You can leverage the Rx operator `filter()` to only subscribe to a subset of emissions.
2017-01-04 19:17:53 +00:00
2017-02-10 17:17:28 +00:00
### Get Refreshing
There is another special way to subscribe to a Store: getRefreshing(key). Get Refreshing will subscribe to get() which returns a single response, but unlike Get, Get Refreshing will stay subscribed. Anytime you call store.clear(key) anyone subscribed to getRefreshing(key) will resubscribe and force a new network response.
2017-02-10 17:17:28 +00:00
2017-01-04 19:17:53 +00:00
### Inflight Debouncer
To prevent duplicative requests for the same data, Store offers an inflight debouncer. If the same request is made within a minute of a previous identical request, the same response will be returned. This is useful for situations when your app needs to make many async calls for the same data at startup or for when users are obsessively pulling to refresh. As an example, The New York Times news app asynchronously calls `ConfigStore.get()` from 12 different places on startup. The first call blocks while all others wait for the data to arrive. We have seen a dramatic decrease in the app's the data usage after implementing this in flight logic.
2017-01-04 19:17:53 +00:00
### Adding a Parser
Since it is rare for data to arrive from the network in the format that your views need, Stores can delegate to a parser by using a `StoreBuilder.<BarCode, BufferedSource, Article>parsedWithKey()
2017-01-04 19:17:53 +00:00
2017-01-05 18:59:48 +00:00
```java
2017-02-10 17:17:28 +00:00
Store<Article,Integer> store = StoreBuilder.<Integer, BufferedSource, Article>parsedWithKey()
.fetcher(articleId -> api.getArticle(articleId))
2017-01-05 14:59:33 +00:00
.parser(source -> {
try (InputStreamReader reader = new InputStreamReader(source.inputStream())) {
return gson.fromJson(reader, Article.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.open();
2017-01-04 19:17:53 +00:00
```
Our updated data flow now looks like this:
`store.get()` -> ![Simple Store Flow](https://github.com/nytm/Store/blob/master/Images/store-3.jpg)
### Middleware - GsonSourceParser
There are also separate middleware libraries with parsers to help in cases where your fetcher is a Reader, BufferedSource or String and your parser is Gson:
- GsonReaderParser
- GsonSourceParser
- GsonStringParser
2017-01-04 19:17:53 +00:00
These can be accessed via a Factory class (GsonParserFactory).
2017-01-04 19:17:53 +00:00
Our example can now be rewritten as:
```java
2017-02-10 17:17:28 +00:00
Store<Article,Integer> store = StoreBuilder.<Integer, BufferedSource, Article>parsedWithKey()
.fetcher(articleId -> api.getArticle(articleId))
.parser(GsonParserFactory.createSourceParser(gson, Article.class))
2017-01-05 14:59:33 +00:00
.open();
2017-01-04 19:17:53 +00:00
```
In some cases you may need to parse a top level JSONArray, in which case you can provide a TypeToken.
```java
2017-02-10 17:17:28 +00:00
Store<List<Article>,Integer> store = StoreBuilder.<Integer, BufferedSource, List<Article>>parsedWithKey()
.fetcher(articleId -> api.getArticles())
.parser(GsonParserFactory.createSourceParser(gson, new TypeToken<List<Article>>() {}))
.open();
2017-02-10 17:17:28 +00:00
```
Similarly we have a middleware artifact for Moshi & Jackson too!
2017-01-04 19:17:53 +00:00
### Disk Caching
Stores can enable disk caching by passing a Persister into the builder. Whenever a new network request is made, the Store will first write to the disk cache and then read from the disk cache.
2017-01-04 19:17:53 +00:00
Now our data flow looks like:
`store.get()` -> ![Simple Store Flow](https://github.com/nytm/Store/blob/master/Images/store-5.jpg)
Ideally, data will be streamed from network to disk using either a BufferedSource or Reader as your network raw type (rather than String).
```java
2017-02-10 17:17:28 +00:00
Store<Article,Integer> store = StoreBuilder.<Integer, BufferedSource, Article>parsedWithKey()
.fetcher(articleId -> api.getArticles())
.persister(new Persister<BufferedSource>() {
@Override
2017-02-10 17:17:28 +00:00
public Observable<BufferedSource> read(Integer key) {
if (dataIsCached) {
2017-02-10 17:17:28 +00:00
return Observable.fromCallable(() -> userImplementedCache.get(key));
} else {
return Observable.empty();
}
}
2017-01-05 14:59:33 +00:00
@Override
public Observable<Boolean> write(BarCode barCode, BufferedSource source) {
2017-02-10 17:17:28 +00:00
userImplementedCache.save(key, source);
return Observable.just(true);
}
})
2017-02-10 17:17:28 +00:00
.parser(GsonParserFactory.createSourceParser(gson, Article.class))
.open();
2017-01-04 19:17:53 +00:00
```
Stores dont care how youre storing or retrieving your data from disk. As a result, you can use Stores with object storage or any database (Realm, SQLite, CouchDB, Firebase etc). The only requirement is that data must be the same type when stored and retrieved as it was when received from your Fetcher. Technically there is nothing stopping you from implementing an in memory cache for the “persister” implementation and instead have two levels of in memory caching--one with inflated and one with deflated models, allowing for sharing of the “persister” cache data between stores.
2017-01-04 19:17:53 +00:00
**Note**: When using a Parser and a disk cache, the Parser will be called AFTER fetching from disk and not between the network and disk. This allows your persister to work on the network stream directly.
2017-01-04 19:17:53 +00:00
If using SQLite we recommend working with SqlBrite. If you are not using SqlBrite, an Observable can be created rather simply with `Observable.fromCallable(() -> getDBValue())`
2017-01-04 19:17:53 +00:00
### Middleware - SourcePersister & FileSystem
We've found the fastest form of persistence is streaming network responses directly to disk. As a result, we have included a separate library with a reactive FileSystem which depends on Okio BufferedSources. We have also included a FileSystemPersister which will give you disk caching and works beautifully with GsonSourceParser. When using the FileSystemPersister you must pass in a `PathResolver` which will tell the file system how to name the paths to cache entries.
Now back to our first example:
2017-01-04 19:17:53 +00:00
2017-01-05 18:59:48 +00:00
```java
2017-02-10 17:17:28 +00:00
Store<Article,Integer> store = StoreBuilder.<Integer, BufferedSource, Article>parsedWithKey()
.fetcher(articleId -> api.getArticles(articleId))
2017-02-10 17:37:37 +00:00
.persister(FileSystemPersister.create(FileSystemFactory.create(context.getFilesDir()),pathResolver))
.parser(GsonParserFactory.createSourceParser(gson, String.class))
2017-01-04 19:17:53 +00:00
.open();
```
As mentioned, the above builder is how we work with network operations at the New York Times. With the above setup you have:
2017-01-04 19:17:53 +00:00
+ Memory caching with Guava Cache
+ Disk caching with FileSystem (you can reuse the same file system implementation for all stores)
2017-02-10 17:17:28 +00:00
+ Parsing from a BufferedSource to a <T> (Article in our case) with Gson
+ In-flight request management
+ Ability to get cached data or bust through your caches (get vs. fresh)
2017-02-10 17:17:28 +00:00
+ Ability to listen for any new emissions from network (stream)
+ Ability to be notified and resubscribed when caches are cleared (helpful for times when you need to do a post request and update another screen, such as with `getRefreshing`)
2017-01-04 19:17:53 +00:00
We recommend using the above builder setup for most Stores. The SourcePersister implementation has a tiny memory footprint because it will stream bytes from network to disk and then from disk to parser. The streaming nature of Stores allows us to download dozens of 1mb+ json responses without worrying about OOM on low-memory devices. As mentioned above, Stores allow us to do things like calling `configStore.get()` a dozen times asynchronously before our Main Activity finishes loading without blocking the main thread or flooding our network.
2017-01-04 19:17:53 +00:00
2017-02-10 17:17:28 +00:00
### RecordProvider
If you'd like your Store to know about disk data staleness, you can have your `Persister` implement `RecordProvider`. After doing so you can configure your Store to work in one of two ways:
```java
2017-02-10 17:17:28 +00:00
store = StoreBuilder.<BufferedSource>barcode()
.fetcher(fetcher)
.persister(persister)
.refreshOnStale()
.open();
```
`refreshOnStale` will backfill the disk cache anytime a record is stale. The user will still get the stale record returned to them.
Or alternatively:
2017-02-10 17:17:28 +00:00
```java
2017-02-10 17:17:28 +00:00
store = StoreBuilder.<BufferedSource>barcode()
.fetcher(fetcher)
.persister(persister)
.networkBeforeStale()
.open();
```
`networkBeforeStale` - Store will try to get network source when disk data is stale. If the network source throws an error or is empty, stale disk data will be returned.
2017-02-10 17:17:28 +00:00
2017-01-04 19:17:53 +00:00
### Subclassing a Store
We can also subclass a Store implementation (RealStore<T>):
```java
2017-02-10 17:17:28 +00:00
public class SampleStore extends RealStore<String, BarCode> {
public SampleStore(Fetcher<String, BarCode> fetcher, Persister<String, BarCode> persister) {
super(fetcher, persister);
}
2017-01-04 19:17:53 +00:00
}
```
2017-01-04 19:17:53 +00:00
Subclassing is useful when youd like to inject Store dependencies or add a few helper methods to a store:
2017-01-04 19:17:53 +00:00
```java
2017-02-10 17:17:28 +00:00
public class SampleStore extends RealStore<String, BarCode> {
2017-01-04 19:17:53 +00:00
@Inject
2017-02-10 17:17:28 +00:00
public SampleStore(Fetcher<String, BarCode> fetcher, Persister<String, BarCode> persister) {
super(fetcher, persister);
}
2017-01-04 19:17:53 +00:00
}
```
### Artifacts
2017-01-26 20:18:48 +00:00
2017-05-23 22:15:13 +00:00
**CurrentVersion = 3.0.0-alpha**
2017-01-26 20:18:14 +00:00
2017-02-10 17:17:28 +00:00
+ **Cache** Cache extracted from Guava (keeps method count to a minimum)
2017-01-04 19:17:53 +00:00
2017-01-05 18:59:48 +00:00
```groovy
2017-05-23 22:15:13 +00:00
compile 'com.nytimes.android:cache3:CurrentVersion'
2017-01-05 18:59:48 +00:00
```
+ **Store** This contains only Store classes and has a dependecy on RxJava + the above cache.
2017-01-04 19:17:53 +00:00
2017-01-05 18:59:48 +00:00
```groovy
2017-05-23 22:15:13 +00:00
compile 'com.nytimes.android:store3:CurrentVersion'
2017-01-05 18:59:48 +00:00
```
2017-02-10 17:17:28 +00:00
+ **Middleware** Sample Gson parsers, (feel free to create more and open PRs)
2017-01-04 19:17:53 +00:00
2017-01-19 14:49:16 +00:00
```groovy
2017-05-23 22:15:13 +00:00
compile 'com.nytimes.android:middleware3:CurrentVersion'
2017-01-19 14:49:16 +00:00
```
2017-02-10 17:17:28 +00:00
+ **Middleware-Jackson** Sample Jackon parsers, (feel free to create more and open PRs)
2017-01-17 16:04:11 +00:00
```groovy
2017-05-23 22:15:13 +00:00
compile 'com.nytimes.android:middleware-jackson3:CurrentVersion'
2017-01-17 16:04:11 +00:00
```
2017-02-10 17:17:28 +00:00
+ **Middleware-Moshi** Sample Moshi parsers, (feel free to create more and open PRs)
2017-01-17 16:04:11 +00:00
2017-01-19 14:49:16 +00:00
```groovy
2017-05-23 22:15:13 +00:00
compile 'com.nytimes.android:middleware-moshi3:CurrentVersion'
2017-01-19 14:49:16 +00:00
```
2017-01-04 19:17:53 +00:00
+ **File System** Persistence Library built using OKIO Source/Sink + Middleware for streaming from Network to FileSystem
2017-01-05 18:59:48 +00:00
```groovy
2017-05-23 22:15:13 +00:00
compile 'com.nytimes.android:filesystem3:CurrentVersion'
2017-01-05 18:59:48 +00:00
```
2017-01-04 19:17:53 +00:00
### Sample Project
See the app for example usage of Store. Alternatively, the Wiki contains a set of recipes for common use cases
2017-01-04 19:17:53 +00:00
+ Simple Example: Retrofit + Store
+ Complex Example: BufferedSource from Retrofit (Can be OKHTTP too) + our FileSystem + our GsonSourceParser
### Talks
[DroidCon Italy](https://youtu.be/TvsOsgd0--c)
[Android Makers](https://www.youtube.com/watch?time_continue=170&v=G1MebI2k9aA)