Issue
I have to use a legacy library that using AsyncTask for a background job. How I can wrap an AsyncTask by an Observable object which I'm using on my current project.
The AsyncTask is encapsulated so I cannot access the synchronous call inside AsyncTask.
Solution
say you have an object asynchronousCall executing some async work with call() method which takes callback as a param, you can wrap it like that :
Observable.create(new Observable.OnSubscribe<Object>() {
@Override
public void call(final Subscriber<? super Object> subscriber) {
asynchronousCall.call(new CallBack() {
@Override
public void success(Object o) {
subscriber.onNext(o);
subscriber.onCompleted();
}
@Override
public void error(Throwable t) {
subscriber.onError(t);
}
});
}
});
Answered By - krp
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.