Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava emit item with minimum delay

I have UserConfig that I would like to download during splash screen.

class UserManager {
    Single<UserConfig> loadConfig()
}

After downloading of the UserConfig, user is redirected to the next screen. I do something like these:

@Override
public void onResume(boolean isNewView) {
    subscriptions.add(
            userManager.loadConfig().subscribe(config -> {
                applyConfig(config);
                launchActivity(HomeActivity.class);
            }, error -> {
                //some error handling
            })
    );
}

However, I would like to show the splash screen for at least 1 second. (If loading took less than 1s add extra delay)

I think .delay(), .delaySubscription() will not work for my case, since they will delay every request (no matter was it shorter that 1s or not).

like image 208
Rostyslav Roshak Avatar asked May 25 '26 00:05

Rostyslav Roshak


1 Answers

Try Zip operator

Returns a Single that emits the results of a specified combiner function > applied to two items emitted by two other Singles.

You can do something like

Single
    .zip(
        Single.timer(1, TimeUnit.SECONDS), 
        userManager.loadConfig(),
        (time, config) -> config
    )
    .subscribe(
        config -> {
            applyConfig(config);
            launchActivity(HomeActivity.class);
        }, error -> {
            //some error handling
        }
     );
like image 77
yst Avatar answered May 27 '26 12:05

yst



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!