Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Rafat J. Al-Barouki
    @rjeeb
    try to do this
    this will execute the two operations in two threads
    so they will be executed in parallel and with no waiting
    and if your method returns a value then you can do this
    Person person = new Person();
            Observable<Person> personObservable = Observable.just(person).subscribeOn(Schedulers.io());
            personObservable.subscribe(p -> {
                // add the object to the database
            });
            // gets the data and return it
    mreddimasi
    @mreddimasi
    @rafatbaroukii Thank you so much, the second approach seems to be fitting my needs will try that. Thanks again
    Keith Hoopes
    @Pytry
    What are some good excercises or games you could play while coding to help you get into the reactive mindset and style of development, but without actually using RxJava?
    Rajan Maurya
    @therajanmaurya
    Hi, I am doing my GSoC project https://github.com/therajanmaurya/android-client-2.0 in which I am using RxJava 2 and handling rest API calls. Initially user logged in and get a access token, I don’t know when this access token will expire so I want to implement a logic that will call, whenever I will get 401 error, that should fire a request to server to refresh the access token and try the current request again. I don’t want check 401 in every request response. I want to implement this in such a way that i don’t need to check 401 reponse code in every rest api call, if any request code is 401 then it will automically make another request to refresh the access token and try the current request again. Please help. It is last feature that need to implement and the most important thing is, I really want to learn how can I do this using RxJava.
    Rajan Maurya
    @therajanmaurya
    Can anyone give me example there these type of logic implemented or something please.
    David Karnok
    @akarnokd
    @therajanmaurya You may have better luck on StackOverflow.
    Rajan Maurya
    @therajanmaurya
    Ronen
    @ronenhamias
    is there example somewhere how to bridge between rx and netty?
    Jacob Kristhammar
    @jacobk
    Any suggestions on how to do the following without relying on errors for control flow?
            Single<RbmAccessToken> accessToken = accessTokens.get(agent);
    
            return Single.just(accessToken)
                .flatMap(t -> t == null
                        ? Single.error(new Throwable("no token"))
                        : t)
                .flatMap(t -> Instant.now().plusSeconds(60).isAfter(t.expiresAt())
                        ? Single.error(new Throwable("token expired"))
                        : Single.just(t))
                .onErrorResumeNext(t -> {
                    Single<RbmAccessToken> newAccessToken = getAccessToken(agent);
                    accessTokens.put(agent, newAccessToken);
                    return newAccessToken;
                });
    hehe, also, the above doesn't work :D
    Artem Rudometkin
    @perfectplayer
    @jacobk, also thought about it. Looks like there is some overRx. Maybe it's better to break up this logic into more pieces.
    Jacob Kristhammar
    @jacobk

    I had the following before:

    if (accessToken != null && accessToken.blockingGet().expiresAt().isAfter(Instant.now().plusSeconds(60))) {
        return accessToken;
    }
    
    Single<RbmAccessToken> newAccessToken = getAccessToken(agent);
    accessTokens.put(agent, newAccessToken);
    return newAccessToken;

    and was trying to get rid of the blocking get

    For completness here's the current version that actually works, didn't know about null not usable as values
    Single<RbmAccessToken> accessToken = accessTokens.get(agent);
    
    return Single.just(Optional.ofNullable(accessToken))
      .flatMap(t -> t.orElse(Single.error(new Throwable("no token"))))
      .flatMap(t -> t.isExpired()
          ? Single.error(new Throwable("token expired"))
          : Single.just(t))
      .onErrorResumeNext(t -> {
        Single<RbmAccessToken> newAccessToken = generateAccessToken(agent);
        accessTokens.put(agent, newAccessToken);
        return newAccessToken;
      });
    Jacob Kristhammar
    @jacobk
    I feel somwhat ok with using error for the expire branch, but the initial null-check feels really contrieved
    Artem Rudometkin
    @perfectplayer
    From the docs: > Note that if you pass null to Just, it will return an Observable that emits null as an item. Do not make the mistake of assuming that this will return an empty Observable (one that emits no items at all). For that, you will need the Empty operator.
    Jacob Kristhammar
    @jacobk
    Doesn't seem to be true for javarx2 it errors out on null values
    @perfectplayer do you mean "overrx" as in trying to do too much with rx?
    Artem Rudometkin
    @perfectplayer
    Yes, but later code looks bit better. If it really works, I think you don't need to change anything
    Fyodor Sherstobitov
    @fsherstobitov

    Hi! Is there a way to pass the output of one Observable to the input of the second in RxJava2? My task is to get the list of users from server and then save it to the DB. I'm using Retrofit2 for API and Room as ORM on Android. Now I have this code

    api.userList()
                    .map(new Function<List<UserDto>, List<User>>() {
                        @Override
                        public List<User> apply(@NonNull List<UserDto> userDtos) throws Exception {
                            return userMapper.fromDtoList(userDtos);
                        }
                    })

    And I need to pass List<User> that I get from map to this method in DAO

    @Insert
        Completable insertAll(List<User> users);
    Fyodor Sherstobitov
    @fsherstobitov
    Found the answer - I must use flatMap to do that
    Benoit Lubek
    @BoD
    Hi!! I'm trying to find a way to reset a BehaviorSubject... Couldn't find a way - any thoughts?
    Binoy Balu
    @beingentangled
    @fsherstobitov Could you share an example?
    neuberfran
    @neuberfran
    what about rxfirebase?
    Neil Okamoto
    @gonewest818
    Does anyone here know of an example calling rxjava2 from clojure via java interop?
    dave08
    @dave08
    I'm accessing a db with vertx and caching the result in the observable so that all other observables can be zipped with its result. But I need to invalidate this cache. It's an api that tons of android devices are connecting to, the data I'm caching stays the same for all of them, but may be changed in the admin so vertx must reload it...
    I'm currently on RxJava 1.x...
    And Kotlin
    dave08
    @dave08
    It would have been nice to have a .cacheWhile operator for this...
    Ilya Gulya
    @IlyaGulya
    Hello everyone!
    How can i solve such problem.
    I have Observable that emits items randomly. (Button clicks, for example)
    I want it to emit first received item within some window (3 seconds, for example) immediately and then skip all other items that emitted during next 3 seconds. After 3 seconds, this behaviour should repeat.
    Something like this: https://codeshare.io/an4LbE
    Artem Rudometkin
    @perfectplayer
    Seems like you want to filter your items.
    Ilya Krol
    @ilya_krol_twitter

    Hi everyone! I am struggling to find a way to execute the command currently in the 'andThen' only when the upstream completable completes successfully without an error. Is there a better way other than saving the error state in a variable, which is quite ugly?

    someValue
        .toSingle()
        .flatMapCompletable { possiblyExceptionCompletable }
        .andThen(updateInteractor(success))
        .onErrorResumeNext { updateInteractor(failure) }

    In this scenario if possiblyExceptionCompletable throws an exception - both the success and failure updates occur.

    Exerosis
    @Exerosis
    if I were to do: test.switchMap(blah) when test completes does the most recent observable get unsubscribed?
    @ilya_krol_twitter
    value.flatMap(ignored -> opThatMightFail()
                                        .map(result -> new State(result)) //result is in
                                        .startWith(new State()) //Loading
                                        .onErrorReturn(throwable -> new State(throwable)))
               .doOnNext(state -> updateInteractor(state))
    Ilya Krol
    @ilya_krol_twitter

    @Exerosis thanks for the response, but this has the same effect, assuming that the outcome of the flatMap is a Completable and I use andThen instead of doOnNext. I could make it doOnNext (doOnComplete actually as the result is a Completable), and this is how i currently solved this, but the updateInteractor is a Completable also, so this breaks the stream somewhat as I subscribe to this different stream in the side effect operator. It works but it looks ugly and I wonder if there is a better option which keeps me within the same stream for all operations. as a clarification this is how my code looks right now (roughly):

    someValue
        .toSingle()
        .flatMapCompletable { possiblyExceptionCompletable }
        .doOnComplete { updateInteractor(success).subscribeOn(Schedulers.io()).subscribe() }
        .doOnError { updateInteractor(failure).subscribeOn(Schedulers.io()).subscribe() }

    I want to get rid of the two subscriptions mid-stream.

    Exerosis
    @Exerosis
    @ilya_krol_twitter May I ask what your ultimate goal is? And is there a reason you couldn't do someValue.flatMap(possiblyExceptionObservable).map(result -> interactorUpdate).toSingle().subOn(io).sub(this::updateInteractor). ? It's hard to fully understand what's happening here to some degree, might be able to provide a more meaningful answer with a few more details.
    Stas Shusha
    @journeyman
    Hi, could anyone please help me understand why this test fails?! In Rx.NET it works perfectly fine and seems logical :) And I couldnt find the explanation in the docs
        @Test
        fun test() {
            val connectable = Observable.just(1,2,3).replay(1)
            val testSubscriber = TestSubscriber.create<Int>()
    
            connectable.connect()
            connectable.subscribe(testSubscriber)
    
            testSubscriber.assertReceivedOnNext(listOf(3))
        }
    the actual result is all 3 items: [1,2,3]
    David Karnok
    @akarnokd
    Because of backpressure. replay() won't request unless there is a consumer for it.
    Stas Shusha
    @journeyman
    found an answer: related to backpressure changes in rxjava 1.0.14
    @akarnokd oh, thankx)
    David Karnok
    @akarnokd
    You are welcome, twice I guess ;)
    Stas Shusha
    @journeyman
    sure)
    though you haven't propose any workarounds
        fun <T> Observable<T>.replayForced() : ConnectableObservable<T> {
            val upstream = this.replay()
            val sub = upstream.subscribe()
            upstream.doOnUnsubscribe { sub.unsubscribe() }
            return upstream
        }