This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// wrapping synchronous operation in an RxJava Observable | |
Observable<Boolean> wipeContents(final SharedPreferences sharedPreferences) { | |
return Observable.fromCallable(new Callable<Boolean>() { | |
@Override | |
public Boolean call() throws Exception { | |
return sharedPreferences.edit().clear().commit(); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DomainService.java | |
@RxLogObservable | |
public Observable<Data> getData() { | |
return getMergedData() | |
.onErrorReturn(new Func1<Throwable, Data>() { | |
@Override | |
public Data call(Throwable throwable) { | |
Log.e(CLASSNAME, "Error while fetching data. Swallowing the exception.", throwable); | |
return null; // We return null since we know our filter will ignore null values. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We assume the repositories getData() methods will return null if no data is available | |
// DomainService.java | |
@RxLogObservable | |
public Observable<Data> getData() { | |
return getMergedData() | |
.filter( | |
new Func1<Data, Boolean>() { | |
@Override | |
public Boolean call(Data data) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DiskRepository.java | |
public void saveData(Data data) { | |
// implementation | |
} | |
// DomainService.java | |
@RxLogObservable | |
public Observable<Data> getMergedData() { | |
return Observable.merge( | |
diskRepository.getData().subscribeOn(Schedulers.io()), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DiskRepository.java | |
@RxLogObservable | |
public Observable<Data> getData() { | |
// implementation | |
} | |
// NetworkRepository.java | |
@RxLogObservable | |
public Observable<Data> getData() { | |
// implementation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This assumes both 'repositories' return Observable<Data> | |
Observable<Data> results = Observable.merge( | |
diskRepository.getData().subscribeOn(Schedulers.io()), | |
networkRepository.getData().subscribeOn(Schedulers.io()) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NetworkRepository.java - no changes | |
@RxLogObservable | |
public Observable<Data> getData() { | |
// implementation | |
} | |
// DiskRepository.java | |
@RxLogObservable | |
public Observable<Timestamped<Data>> getData() { | |
// implementation |
NewerOlder