Skip to content

Reusing ConnectableObservable created using replay().autoConnect() #6227

@ghost

Description

I was making a component to reduce parallel requests for a common resource by using replay(). In the following example:

val f = Flowable.fromCallable { "FOO" }.replay(10, TimeUnit.SECONDS).autoConnect()
f.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
                { println("Result 1 $it") },
                { println("Error 1 $it") },
                { println("Complete 1") } )
f.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
                { println("Result 2 $it") },
                { println("Error 2 $it") },
                { println("Complete 2") } )
Flowable.timer(11, TimeUnit.SECONDS)
        .flatMap { f }
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
                { println("Result 3 $it") },
                { println("Error 3 $it") },
                { println("Complete 3") } )

This gives

Result 1 FOO
Complete 1
Result 2 FOO
Complete 2
Complete 3

How can retrigger original flowable if someone subscribes after 10 seconds?

Using RxJava 2.x

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions