第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

RxJava Single.just() vs Single.fromCallable()?

RxJava Single.just() vs Single.fromCallable()?

富國(guó)滬深 2021-10-28 15:54:52
我想知道是否有人可以闡明這個(gè)問(wèn)題,何時(shí)使用Single.fromCallable( ()-> myObject )代替Single.just(myObject)從文檔中,Single.fromCallable(): /** * Returns a {@link Single} that invokes passed function and emits its result for each new SingleObserver that subscribes. * <p> * Allows you to defer execution of passed function until SingleObserver subscribes to the {@link Single}. * It makes passed function "lazy". * Result of the function invocation will be emitted by the {@link Single}. * <dl> *   <dt><b>Scheduler:</b></dt> *   <dd>{@code fromCallable} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param callable *         function which execution should be deferred, it will be invoked when SingleObserver will subscribe to the {@link Single}. * @param <T> *         the type of the item emitted by the {@link Single}. * @return a {@link Single} whose {@link SingleObserver}s' subscriptions trigger an invocation of the given function. */和文檔Single.just(): /** * Returns a {@code Single} that emits a specified item. * <p> * <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Single.just.png" alt=""> * <p> * To convert any object into a {@code Single} that emits that object, pass that object into the * {@code just} method. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code just} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param item *            the item to emit * @param <T> *            the type of that item * @return a {@code Single} that emits {@code item} * @see <a href="http://reactivex.io/documentation/operators/just.html">ReactiveX operators documentation: Just</a> */
查看完整描述

3 回答

?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

在您提到的用例中,實(shí)際上沒(méi)有重大區(qū)別。


現(xiàn)在想象一下我們需要通過(guò)函數(shù)調(diào)用動(dòng)態(tài)創(chuàng)建對(duì)象嗎?


fun getTimeObject() {

    val timeInMillis = System.currentTimeMillis()

    return TimeObject(timeInMillis)

}

然后,當(dāng)它有一個(gè)新訂閱者時(shí),Single.just(getTimeObject())結(jié)果Single將發(fā)出相同的信息Long。


然而,當(dāng)它有一個(gè)新訂閱者時(shí)Single.fromcallable(()-> getTimeObject()),結(jié)果Single將發(fā)出一個(gè)不同的Long指示以毫秒為單位的當(dāng)前時(shí)間。


那是因?yàn)閒romCallable每當(dāng)它有一個(gè)新訂閱者Lazily 時(shí),它就會(huì)執(zhí)行它的 lambda 。


查看完整回答
反對(duì) 回復(fù) 2021-10-28
?
青春有我

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊

當(dāng)你有一個(gè)類似的函數(shù)時(shí),你應(yīng)該使用fromCallable()


MyObject myFunction() {

    // some login here

    return new MyObject();

}

然后你可以像這樣從這個(gè)函數(shù)創(chuàng)建Single:


Single.fromCallable(() -> myFunction());

Single.just(myObject)只是在沒(méi)有任何邏輯的情況下發(fā)出您的對(duì)象。


因此,當(dāng)您想要發(fā)出特定項(xiàng)目時(shí),無(wú)需使用fromCallable()。


查看完整回答
反對(duì) 回復(fù) 2021-10-28
  • 3 回答
  • 0 關(guān)注
  • 538 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)