Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hot Completable with long running tasks

By default Completable is not hot. But I have case where hot Completable would be really useful: downloading and caching set of large files:

class DownloadManager {
  Completable downloadAndCacheA();
  Completable downloadAndCacheB();
}

Completable.merge(
  downloadManager.downloadAndCacheA(),
  downloadManager.downloadAndCacheB()
).subscribe();

When files are large user can leave the app and turn back. So client code can unsubscribe from these completables and subscribe again. One file can be downloaded, but another is still in progress. But as Completable is not hot downloading starts again.

Possible options is to:

  • Make hot Completable.
  • Use hot Observable which returns Object.
  • Use just Thread instance with custom Completable which interact with each other.

Don't really like all these solutions because of different reasons. Is there better way to support long running completable tasks?

like image 401
eleven Avatar asked Aug 11 '26 21:08

eleven


1 Answers

Using cache, suggested by akarnokd, is probably the easiest way.

public Completable downloadAndCache() {
   if (completable == null) {
          completable = Completable.fromAction(this::syncDownloadAndCache)
                                   .cache();
   }
   return completable;
}
like image 88
eleven Avatar answered Aug 13 '26 12:08

eleven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!