From 0064aa1ef5dfeceabd3f87614ce850cef58d8334 Mon Sep 17 00:00:00 2001 From: Thibaud Giovannetti Date: Thu, 25 Jan 2018 14:52:09 +0100 Subject: [PATCH] Remove useless concat delay error to use boolean in observeOn --- .../store2store/store/StoreService.java | 146 +++++++----------- 1 file changed, 54 insertions(+), 92 deletions(-) diff --git a/store2store/src/main/java/com/playmoweb/store2store/store/StoreService.java b/store2store/src/main/java/com/playmoweb/store2store/store/StoreService.java index 6a2fb73..8103bcb 100644 --- a/store2store/src/main/java/com/playmoweb/store2store/store/StoreService.java +++ b/store2store/src/main/java/com/playmoweb/store2store/store/StoreService.java @@ -29,14 +29,6 @@ public abstract class StoreService extends StoreDao { */ private final StoreDao dao; - /** - * If true, this Service will be used as a cache (first call). - * It's a read-only feature (getAll, getById, getOne, ...). - * - * @warning If this service is used as a cache and its access is slow, it will reduce drastically the performances of syncing - */ - private final boolean isCache; - /** * List of store synced with this one */ @@ -48,19 +40,11 @@ public abstract class StoreService extends StoreDao { protected final CompositeDisposable compositeDisposable = new CompositeDisposable(); /** - * Create a typed Store + * Create a typed store */ public StoreService(Class clazz, StoreDao dao) { - this(clazz, dao, false); - } - - /** - * Create a typed store with cache information - */ - public StoreService(Class clazz, StoreDao dao, boolean isCache) { this.clazz = clazz; this.dao = dao; - this.isCache = isCache; } /** @@ -96,10 +80,6 @@ public boolean hasSyncedStore() { return syncedStore != null; } - public boolean isCache() { - return isCache; - } - @Override public Flowable>> getAll(final Filter filter, final SortingMode sortingMode) { Flowable>> flowStorage = dao.getAll(filter, sortingMode); @@ -136,11 +116,7 @@ public Flowable>> apply(Optional> items) throws Excepti flowables.add(syncedStore.getAll(filter, sortingMode)); flowables.add(flowStorage); - if (syncedStore.isCache()) { - return Flowable.concatDelayError(flowables); - } else { - return Flowable.concat(flowables); - } + return Flowable.concat(flowables); } @Override @@ -161,11 +137,7 @@ public Flowable>> apply(Optional> items) throws Excepti flowables.add(syncedStore.getAll(items)); flowables.add(flowStorage); - if (syncedStore.isCache()) { - return Flowable.concatDelayError(flowables); - } else { - return Flowable.concat(flowables); - } + return Flowable.concat(flowables); } public final Flowable>> getAll(final Filter filter) { @@ -194,11 +166,7 @@ public Flowable> apply(Optional item) throws Exception { flowables.add(syncedStore.getOne(filter, sortingMode)); flowables.add(flowStorage); - if (syncedStore.isCache()) { - return Flowable.concatDelayError(flowables); - } else { - return Flowable.concat(flowables); - } + return Flowable.concat(flowables); } @Override @@ -219,11 +187,7 @@ public Flowable> apply(Optional item) throws Exception { flowables.add(syncedStore.getOne(item)); flowables.add(flowStorage); - if (syncedStore.isCache()) { - return Flowable.concatDelayError(flowables); - } else { - return Flowable.concat(flowables); - } + return Flowable.concat(flowables); } public Flowable> getOne(final Filter filter) { @@ -255,74 +219,72 @@ public Flowable> apply(final Optional item) throws Exception { flowables.add(syncedStore.getById(id)); flowables.add(flowStorage); - if (syncedStore.isCache()) { - return Flowable.concatDelayError(flowables); - } else { - return Flowable.concat(flowables); - } + return Flowable.concat(flowables); } @Override public Flowable>> insert(final List items) { - List>>> flowables = new ArrayList<>(); Flowable>> flowStorage = dao.insert(items); - - if (hasSyncedStore()) { - flowStorage = flowStorage - .onErrorResumeNext(new Function>>>() { - @Override - public Flowable>> apply(final Throwable throwable) throws Exception { - return syncedStore.delete(items).flatMap(new Function>>>() { - @Override - public Flowable>> apply(Object o) throws Exception { - return Flowable.error(throwable); - } - }); - } - }) - .flatMap(new Function>, Flowable>>>() { - @Override - public Flowable>> apply(Optional> it) throws Exception { - return syncedStore.insertOrUpdate(it.get()); - } - }); - - flowables.add(syncedStore.insert(items)); + if (!hasSyncedStore()) { + return flowStorage; } + final List>>> flowables = new ArrayList<>(); + flowStorage = flowStorage + .onErrorResumeNext(new Function>>>() { + @Override + public Flowable>> apply(final Throwable throwable) throws Exception { + return syncedStore.delete(items).flatMap(new Function>>>() { + @Override + public Flowable>> apply(Object o) throws Exception { + return Flowable.error(throwable); + } + }); + } + }) + .flatMap(new Function>, Flowable>>>() { + @Override + public Flowable>> apply(Optional> it) throws Exception { + return syncedStore.insertOrUpdate(it.get()); + } + }); + + flowables.add(syncedStore.insert(items)); flowables.add(flowStorage); + return Flowable.concat(flowables); } @Override public Flowable> insert(final T item) { - List>> flowables = new ArrayList<>(); Flowable> flowStorage = dao.insert(item); - - if (hasSyncedStore()) { - flowStorage = flowStorage - .onErrorResumeNext(new Function>>() { - @Override - public Flowable> apply(final Throwable throwable) throws Exception { - return syncedStore.delete(item).flatMap(new Function>>() { - @Override - public Flowable> apply(Integer zeroOrOne) throws Exception { - return Flowable.error(throwable); - } - }); - } - }) - .flatMap(new Function, Flowable>>() { - @Override - public Flowable> apply(Optional it) throws Exception { - return syncedStore.insertOrUpdate(it.get()); - } - }); - - flowables.add(syncedStore.insert(item)); + if (!hasSyncedStore()) { + return flowStorage; } + final List>> flowables = new ArrayList<>(); + flowStorage = flowStorage + .onErrorResumeNext(new Function>>() { + @Override + public Flowable> apply(final Throwable throwable) throws Exception { + return syncedStore.delete(item).flatMap(new Function>>() { + @Override + public Flowable> apply(Integer zeroOrOne) throws Exception { + return Flowable.error(throwable); + } + }); + } + }) + .flatMap(new Function, Flowable>>() { + @Override + public Flowable> apply(Optional it) throws Exception { + return syncedStore.insertOrUpdate(it.get()); + } + }); + + flowables.add(syncedStore.insert(item)); flowables.add(flowStorage); + return Flowable.concat(flowables); }