I'm trying to upload a file via Amazon's S3 Android SDK. I've used RX Java a bit but I'm not sure how to convert this method to a method that returns an Observable because I want to chain the result of this method to another Observable call. It confuses me I suppose because of the fact that this does not return right away and can't return until either OnError or OnState changes. How do I handle these situations in an RX way?
public void uploadFile(TransferObserver transferObserver){
transferObserver.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
}
@Override
public void onError(int id, Exception ex) {
}
});
}
If someone could answer with RX Java 2 and lambdas that would be great because I just keep coming up short on this one
This is generally the right approach to bridge between the async/callback workd to reactive, but using
Observable.create()
is now discouraged, as it's requires advanced knowledge in order to make it right.You should use more recent create method
Observable.fromEmitter()
, which will look quite the same:What was added here is: dealing with unsusbcription: cancelling the upload, and unregistering to avoid memory leaks, and specifying backpressure strategy.
you can read more here.
additional notes:
onProgressChanged()
and convert the Observable toObservable<Integer>
.Completable
which is Observable with noonNext()
emissions but onlyonCompleted()
this can suits your case if your not interested with progress indications.@Yosriz I could not get your code to compile , but you did help me quite a bit so based on your answer here is what I now have: