0
votes
mDisposable = mAdapter.getPublisher()
.subscribeOn(Schedulers.io())
.map(new Function<CreateVideoRx, CreateVideoRx>() {

    @Override
    public CreateVideoRx apply(CreateVideoRx createVideoRx) throws Exception {
        Bitmap bitmap = mMediaMetadataRetriever.getFrameAtTime(createVideoRx.time * 1000000, MediaMetadataRetriever.OPTION_CLOSEST);
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, 50, 90);
        createVideoRx.mBitmap = bitmap;
        return createVideoRx;
    }
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<CreateVideoRx>() {
    @Override
    public void accept(CreateVideoRx createVideoRx) throws Exception {
        createVideoRx.mImageView.setImageBitmap(createVideoRx.mBitmap);
    }
});

This RxJava chain works. But I am still doing something wrong because it lags and it does not feel like it is working on a background thread. What will be done in the IO thread in this case and what will be done in the MainThread?

I have done this before with an AsyncTask. It worked fine but now I wanted to skip that and use RxJava instead. The result I have is working but it lags a lot.

Edit: Added some more information

  private final PublishSubject<CreateVideoRx> mPublisher = PublishSubject.create();

Above object is called in the mAdapter.getPublisher() and the function itself looks like

  public PublishSubject<CreateVideoRx> getPublisher() {
    return mPublisher;
  }

What i want to do is to Extract a thumbnail on the background thread. Then when it is done i want it to be pushed to a single ImageView.

2
Could be due to a race condition on createVideoRx; looks like you share the same instance between multiple threads instead of sending immutable data, like the thumbnail itself to the GUI thread. - akarnokd
which code blocks do you expect to be done on io thread? - yosriz
i was hoping that i could make the first block be executed in the io thread. But noticed this morning that both are executed in main. - Jemil Riahi
What is mAdapter.getPublisher()? - Divers
added some more information - Jemil Riahi

2 Answers

1
votes

"In RxJava, you can tell your Observable code which thread to run on using subscribeOn(), and which thread your Subscriber should run on using observeOn()". This is however complicated by the fact that operators subscribe to the source observable.

The way I keep it straight is remember that subscribeOn() affects everything upstream of the function while observeOn affects everything downstream of the function. What you should have done in the original question is

mDisposable = mAdapter.getPublisher()    
.map(new Function<CreateVideoRx, CreateVideoRx>() {

    @Override
    public CreateVideoRx apply(CreateVideoRx createVideoRx) throws Exception {
        Bitmap bitmap = mMediaMetadataRetriever.getFrameAtTime(createVideoRx.time * 1000000, MediaMetadataRetriever.OPTION_CLOSEST);
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, 50, 90);
        createVideoRx.mBitmap = bitmap;
        return createVideoRx;
    }
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<CreateVideoRx>() {
    @Override
    public void accept(CreateVideoRx createVideoRx) throws Exception {
        createVideoRx.mImageView.setImageBitmap(createVideoRx.mBitmap);
    }
});
0
votes
        mDisposable = mAdapter.getPublisher()
            .observeOn(Schedulers.io())
            .map(new Function<CreateVideoRx, CreateVideoRx>() {
                @Override
                public CreateVideoRx apply(CreateVideoRx createVideoRx) throws Exception {
                    Bitmap bitmap = mMediaMetadataRetriever.getFrameAtTime(createVideoRx.time * 1000000, MediaMetadataRetriever.OPTION_CLOSEST);
                    bitmap = ThumbnailUtils.extractThumbnail(bitmap, 50, 90);
                    createVideoRx.mBitmap = bitmap;
                    return createVideoRx;
                }
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<CreateVideoRx>() {
                @Override
                public void accept(CreateVideoRx createVideoRx) throws Exception {
                    createVideoRx.mImageView.setImageBitmap(createVideoRx.mBitmap);
                }
            });

This solved my issue. But i still do not understand fully the difference between SubscribeOn and ObserveOn