简体   繁体   中英

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).

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
        }
     );

My solution with kotlin extention function for Single type. This delay work similarly with errors

/**
 * sets the minimum delay on the success or error
 */
fun <T> Single<T>.minDelay(time: Long, unit: TimeUnit, scheduler: Scheduler = Schedulers.computation()): Single<T> {
    val timeStart = scheduler.now(TimeUnit.MILLISECONDS)
    val delayInMillis = TimeUnit.MILLISECONDS.convert(time, unit)
    return Single.zip(
        Single.timer(time, unit, scheduler),
        this.onErrorResumeNext { error: Throwable ->
            val afterError = scheduler.now(TimeUnit.MILLISECONDS)
            val millisPassed = afterError - timeStart

            val needWaitDelay = delayInMillis - millisPassed
            if (needWaitDelay > 0)
                Single.error<T>(error)
                    .delay(needWaitDelay, TimeUnit.MILLISECONDS, scheduler, true)
            else
                Single.error<T>(error)
        },
        BiFunction { _, t2 -> t2 }
    )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM