rx java2 - Auto-complete a Single created from another Observable -
i have long-running operation returns value in code don't control. need value published things ask it. purpose using behaviorsubject
:
var subject: subject<value>? = null fun retrievevalue(): single<value> { if (subject == null) { subject = behaviorsubject.create<value>() someotherthing.retrievevalueasync { value -> subject.onnext(value) } } return subject.singleorerror() }
this lets me perform operation once , send result single future interested parties. however, not work. single not emit value until call:
subject.oncomplete()
but problem because once subject completed future things can no longer subscribe it.
what appropriate way cache value observable , pass single? if there way have subject automatically complete once source observable emitted value work. single.cache()
looks promising, i'm unsure how handle fact value comes in asynchronously in case.
it feels i'm missing silly.
there singlesubject
case.
if don't want experimental code in codebase, can use replaysubject.createwithsize(1)
, call oncomplete
without losing last value, convert single
.
wiki
Comments
Post a Comment