update readme with jsonarray example (#53)
This commit is contained in:
parent
8870e4f235
commit
3f0f50ced7
1 changed files with 31 additions and 22 deletions
53
README.md
53
README.md
|
@ -132,10 +132,11 @@ Our updated data flow now looks like this:
|
|||
### Middleware - GsonSourceParser
|
||||
|
||||
There is also a seperate middleware lib with parsers to help in cases where your fetcher is a Reader, BufferedSource or String and your parser is Gson:
|
||||
GsonReaderParser,
|
||||
GsonSourceParser,
|
||||
GsonStringParser.
|
||||
- GsonReaderParser
|
||||
- GsonSourceParser
|
||||
- GsonStringParser
|
||||
|
||||
These can be accessed via a Factory class (GsonParserFactory).
|
||||
|
||||
Our example can now be rewritten as:
|
||||
```java
|
||||
|
@ -145,6 +146,14 @@ Store<Article> Store = ParsingStoreBuilder.<BufferedSource, Article>builder()
|
|||
.open();
|
||||
```
|
||||
|
||||
In some cases you may need to parse a top level JSONArray, in which case you can provide a TypeToken.
|
||||
```java
|
||||
Store<List<Article>> Store = ParsingStoreBuilder.<BufferedSource, List<Article>>builder()
|
||||
.nonObservableFetcher(this::getResponse)
|
||||
.parser(GsonParserFactory.createSourceParser(gson, new TypeToken<List<Article>>() {}))
|
||||
.open();
|
||||
```
|
||||
|
||||
### Disk Caching
|
||||
|
||||
Stores can enable disk caching by passing in a Persister to the builder. Whenever a new network request is made, it will first write to the disk cache and then read from the disk cache.
|
||||
|
@ -158,26 +167,26 @@ Now our data flow looks like:
|
|||
Ideally, data will be streamed from network to disk using either a BufferedSource or Reader as your network raw type (rather than String).
|
||||
|
||||
```java
|
||||
Store<String> Store = ParsingStoreBuilder.<BufferedSource, String>builder()
|
||||
.nonObservableFetcher(this::ResponseAsSource) //OkHttp responseBody.source()
|
||||
.persister(new Persister<BufferedSource>() {
|
||||
@Override
|
||||
public Observable<BufferedSource> read(BarCode barCode) {
|
||||
if (dataIsCached) {
|
||||
return Observable.fromCallable(() -> userImplementedCache.get(barCode));
|
||||
} else {
|
||||
return Observable.empty();
|
||||
}
|
||||
}
|
||||
Store<String> Store = ParsingStoreBuilder.<BufferedSource, String>builder()
|
||||
.nonObservableFetcher(this::ResponseAsSource) //OkHttp responseBody.source()
|
||||
.persister(new Persister<BufferedSource>() {
|
||||
@Override
|
||||
public Observable<BufferedSource> read(BarCode barCode) {
|
||||
if (dataIsCached) {
|
||||
return Observable.fromCallable(() -> userImplementedCache.get(barCode));
|
||||
} else {
|
||||
return Observable.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<Boolean> write(BarCode barCode, BufferedSource source) {
|
||||
userImplementedCache.save(barCode, source);
|
||||
return Observable.just(true);
|
||||
}
|
||||
})
|
||||
.parser(GsonParserFactory.createSourceParser(gson, String.class))
|
||||
.open();
|
||||
@Override
|
||||
public Observable<Boolean> write(BarCode barCode, BufferedSource source) {
|
||||
userImplementedCache.save(barCode, source);
|
||||
return Observable.just(true);
|
||||
}
|
||||
})
|
||||
.parser(GsonParserFactory.createSourceParser(gson, String.class))
|
||||
.open();
|
||||
```
|
||||
|
||||
Stores don’t care how you’re 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 you can store and retrieve the data using the same type as your Fetcher. Technically there is nothing stopping you from implementing an in memory cache for the “persister” implementation and instead have 2 levels of in memory caching (one with inflated and one with deflated models, allowing for sharing of the “persister” cache data between stores).
|
||||
|
|
Loading…
Reference in a new issue