2015-09-05 112 views

回答

3

是的,當你完全理解邏輯之後,它是可能的,而且非常容易。但你可能混淆了一下observeOn()和subscribeOn()運算符:)

uiObservable 
    .switchMap(o -> return anotherUIObservable) 
    .subscribeOn(AndroidSchedulers.mainThread()) // means that the uiObservable and the switchMap above will run on the mainThread. 

    .switchMap(o -> return networkObservable) //this will also run on the main thread 

    .subscribeOn(Schedulers.newThread()) // this does nothing as the above subscribeOn will overwrite this 

    .observeOn(AndroidSchedulers.mainThread()) // this means that the next operators (here only the subscribe will run on the mainThread 
    .subscribe(result -> doSomething(result)) 

也許這是你想要什麼:

uiObservable 
    .switchMap(o -> return anotherUIObservable) 
    .subscribeOn(AndroidSchedulers.mainThread()) // run the above on the main thread 

    .observeOn(Schedulers.newThread()) 
    .switchMap(o -> return networkObservable) // run this on a new thread 

    .observeOn(AndroidSchedulers.mainThread()) // run the subscribe on the mainThread 
    .subscribe(result -> doSomething(result)) 

獎勵:我已經寫a post這些運營商,希望它有助於

+0

謝謝!我現在有一系列的跟進問題,我會在我抽出時間的時候提出。 –

+0

@SaadFarooq隨時問:) – Diolor