question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RxJava extension: favor Observable.defer + ReplaySubject over Observable.create

See original GitHub issue

Hi @kittinunf 😃

Creating an Observable using its static built-in create method could lead to unexpected results, and because of that its usage is generally disallowed.

This method requires advanced knowledge about building operators and data sources; please consider other standard methods first

Extracted from the official docs.

That’s why I would recommend to change the current implementation to use defer and ReplaySubject:

private fun <T : Any> Request.rx_response(deserializable: Deserializable<T>): Observable<Pair<Response, T>> =
        Observable.defer {
            val source = ReplaySubject.create<Pair<Response, T>>()
            response(deserializable) { request, response, result ->
                when (result) {
                    is Result.Success -> {
                        source.onNext(response to result.value)
                        source.onCompleted()
                    }

                    is Result.Failure -> {
                        source.onError(result.error)
                    }
                }
            }
            source.doOnSubscribe { this.cancel() }
        }

If you agree with this proposal I could make a PR 😃

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kittinunfcommented, Mar 13, 2017

This should be closed then! Thanks @VictorAlbertos so much for helping me out ❤️

0reactions
VictorAlbertoscommented, Mar 9, 2017

Of course 😃 Already done at #138

Read more comments on GitHub >

github_iconTop Results From Across the Web

RxJava extension: favor Observable.defer + ReplaySubject ...
It emits to any observer all of the items that were emitted by the source Observable(s). Extracted from the Subject official docs. On...
Read more >
Difference between Observable.defer and Observable.create ...
So the distinction seems to be: defer is good when you have something that creates/returns an observable already, but you don't want that ......
Read more >
4. Applying Reactive Programming to Existing Applications
The underlying Observable is eager, so we want to postpone its creation. defer() will wait until the last possible moment to actually create...
Read more >
Reactive Extensions for the JVM: RxJava Part 2 - Medium
The just method creates an Observable that will emit a predifined sequence of values, supplied on creation, and then terminate. Observable<String> values = ......
Read more >
Breaking Changes in Version 7 - RxJS
It was NEVER documented that end users of the library should be creating operators using lift . ... Until the end of v7,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found