Search code examples
androidrx-javabehaviorsubject

Rx Interval between calling PublishSubject.onNext() and receiving it is long


In background thread I call PublishSubject.onNext(); and in Main thread via subscribe(PublishSubject.filter(message -> message.getId() == id), this::onMessageInserted); I receive it. The average duration takes around 20 milliseconds. Is there a way to make this time less?

we use 'io.reactivex:rxjava:1.2.5' 'io.reactivex:rxandroid:1.2.1'


Solution

  • I don't think that it would be possible.

    The main problem is that to deliver an item to main thread from the background the main looper is used.

    It performs all system actions, such as layouting, drawing, calling lifecycle methods, and after the looper finished all that actions, It runs actions added to its queue.

    When you use some schedulers to switch to main thread, every time it gets an item it would add runnable to looper queue, but as you can see that runnable will not be called immediately, but in the end of the Loopers loop.

    The time 20ms is close to the average drawing time(the frequency with wich main looper works).