Created
November 13, 2017 05:47
-
-
Save dyguests/a632cd4c05025220fe9eabafe4150208 to your computer and use it in GitHub Desktop.
RxJava2 + LiveData
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fanhl.architecturedemo.rxjava2 | |
import android.arch.lifecycle.LifecycleOwner | |
import android.arch.lifecycle.MutableLiveData | |
import com.fanhl.architecturedemo.extensions.observe | |
import io.reactivex.Observable | |
/** | |
* desc: | |
* date: 2017/11/13 | |
* | |
* @author fanhl | |
*/ | |
class RxLiveData<T> : MutableLiveData<T>() { | |
fun asObservable(owner: LifecycleOwner): Observable<T> { | |
return Observable.create<T> { emitter -> | |
observe(owner) { data -> | |
try { | |
if (data != null) { | |
emitter.onNext(data) | |
} else { | |
emitter.onError(NullPointerException("com.fanhl.architecturedemo.rxjava2.RxLiveData.asObservable中发送的数据不能为null")) | |
} | |
} catch (e: Exception) { | |
emitter.onError(e) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
话说 Observable 的生命周期咋个办呢?