0

I'm trying to use RxJava to create an Observable (or Flowable) that gets its items from a database table. It should be considered a cold observable, in that each new subscriber can get all available items if it wants. It is also an infinite stream, as new items can get appended to the table.

Processing of the items by a subscriber may take a long time, and may potentially need to be resumed after a restart of the system. For this purpose, subscribers keep track of where they are in the stream. Each item has an index associated with it which subscribers can track.

What I don't understand however is how I could resume such a subscription without reading previous items from the table. I understand that I can create the subscription, then call skip, but if we're going to skip a million items, this seems like a big waste of resources.

It seems like I could create my own subscribe method which takes an additional parameter that specifies the start index, but I'm unsure how to proceed:

void subscribe(Observer<? super T> observer, long offset) {
    subscribe(new WrapWithOffset(observer, offset));
}

Where WrapWithOffset is an Observer that I could check for in subscribeActual:

  protected void subscribeActual(@NonNull Observer<? super String> observer) {
      if (observer instanceof WrapWithOffset wwo) {
          // get offset out, use it to skip items
      }
      else {
          // start from the beginning
      }
  }

This feels a bit hacky. Could it be done better? Is RxJava even intended for such scenario's?

john16384
  • 7,800
  • 2
  • 30
  • 44

0 Answers0