Issue
I am trying to convert the following 2 asynctasks to rxjava, but not sure how to go about it. Any ideas? :
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
/* Shutdown video players */
Set<Map.Entry<String, PlayerBundle>> entries = videoPlayerPool.entrySet();
for (Map.Entry<String, PlayerBundle> entry : entries) {
PlayerBundle bundle = entry.getValue();
bundle.player.release();
}
/* Shutdown audio players */
entries = audioPlayerPool.entrySet();
for (Map.Entry<String, PlayerBundle> entry : entries) {
PlayerBundle bundle = entry.getValue();
bundle.player.release();
}
videoStreamer.stopStream();
videoStreamer.release();
audioStreamer.stopStream();
audioStreamer.release();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
cb.event(new Spin.Event<Void>());
}
}.execute();
and :
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
Set<Map.Entry<String, Participant>> entries = pool.entrySet();
for (Map.Entry<String, Participant> entry : entries) {
Participant participant = entry.getValue();
participant.release();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
cb.event(new Spin.Event<Void>());
}
}.execute();
I have already included the rxjava in my gradle, but not sure how to go about converting this
Solution
Observable.defer(new Func0<Observable<Void>>() {
@Override
public Observable<Void> call() {
/* Shutdown video players */
Set<Map.Entry<String, PlayerBundle>> entries = videoPlayerPool.entrySet();
for (Map.Entry<String, PlayerBundle> entry : entries) {
PlayerBundle bundle = entry.getValue();
bundle.player.release();
}
/* Shutdown audio players */
entries = audioPlayerPool.entrySet();
for (Map.Entry<String, PlayerBundle> entry : entries) {
PlayerBundle bundle = entry.getValue();
bundle.player.release();
}
videoStreamer.stopStream();
videoStreamer.release();
audioStreamer.stopStream();
audioStreamer.release();
return Observable.just(null);
}
})
.doOnCompleted(new Action0() {
@Override
public void call() {
cb.event(new Spin.Event<Void>());
}
})
.subscribeOn(Schedulers.computation())
.subscribe();
Observable.defer(new Func0<Observable<Void>>() {
@Override
public Observable<Void> call() {
Set<Map.Entry<String, Participant>> entries = pool.entrySet();
for (Map.Entry<String, Participant> entry : entries) {
Participant participant = entry.getValue();
participant.release();
}
return Observable.just(null);
}
}).doOnCompleted(new Action0() {
@Override
public void call() {
cb.event(new Spin.Event<Void>());
}
})
.subscribeOn(Schedulers.computation())
.subscribe();
Answered By - Aleksey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.