2015-09-05 73 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