Created
July 11, 2017 08:31
-
-
Save iammert/572a2d5cb775a1dc1a2656e33fa49715 to your computer and use it in GitHub Desktop.
NetworkBoundSource.java
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
public abstract class NetworkBoundSource<LocalType, RemoteType> { | |
public NetworkBoundSource(FlowableEmitter<Resource<LocalType>> emitter) { | |
Disposable firstDataDisposable = getLocal() | |
.map(Resource::loading) | |
.subscribe(emitter::onNext); | |
getRemote().map(mapper()) | |
.subscribeOn(Schedulers.newThread()) | |
.observeOn(Schedulers.newThread()) | |
.subscribe(localTypeData -> { | |
firstDataDisposable.dispose(); | |
saveCallResult(localTypeData); | |
getLocal().map(Resource::success).subscribe(emitter::onNext); | |
}); | |
} | |
public abstract Single<RemoteType> getRemote(); | |
public abstract Flowable<LocalType> getLocal(); | |
public abstract void saveCallResult(LocalType data); | |
public abstract Function<RemoteType, LocalType> mapper(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At line 11 ditn't you have to have an "OnError' handler?
from:
to: