Android Studio 3.0 Beta2
我创建了两个方法,一个创建了observable,另一个创建了订阅者。
但是,我遇到了一个问题,试图让订阅者订阅observable。在Java中,这将起作用,我试图让它在Kotlin中工作。
在我的onCreate(..)方法中,我试图设置它。这是正确的方法吗?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/* CANNOT SET SUBSCRIBER TO SUBCRIBE TO THE OBSERVABLE */
createStringObservable().subscribe(createStringSubscriber())
}
fun createStringObservable(): Observable<String> {
val myObservable: Observable<String> = Observable.create {
subscriber ->
subscriber.onNext("Hello, World!")
subscriber.onComplete()
}
return myObservable
}
fun createStringSubscriber(): Subscriber<String> {
val mySubscriber = object: Subscriber<String> {
override fun onNext(s: String) {
println(s)
}
override fun onComplete() {
println("onComplete")
}
override fun onError(e: Throwable) {
println("onError")
}
override fun onSubscribe(s: Subscription?) {
println("onSubscribe")
}
}
return mySubscriber
}
}
非常感谢任何建议,
答案 0 :(得分:6)
密切注意类型。
Observable.subscribe()
有三个基本变体:
io.reactivex.functions.Consumer
io.reactivex.Observer
您在示例中尝试订阅的类型是org.reactivestreams.Subscriber
(定义为Reactive Streams规范的一部分)。您可以refer to the docs更全面地了解这种类型,但足以说它与任何重载的Observable.subscribe()
方法都不兼容。
这是一个允许代码编译的createStringSubscriber()
方法的修改示例:
fun createStringSubscriber(): Observer<String> {
val mySubscriber = object: Observer<String> {
override fun onNext(s: String) {
println(s)
}
override fun onComplete() {
println("onComplete")
}
override fun onError(e: Throwable) {
println("onError")
}
override fun onSubscribe(s: Disposable) {
println("onSubscribe")
}
}
return mySubscriber
}
改变的是:
Observer
类型(而不是Subscriber
)onSubscribe()
传递Disposable
(而不是Subscription
)..正如'Vincent Mimoun-Prat'所提到的,lambda语法可以真正缩短您的代码。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Here's an example using pure RxJava 2 (ie not using RxKotlin)
Observable.create<String> { emitter ->
emitter.onNext("Hello, World!")
emitter.onComplete()
}
.subscribe(
{ s -> println(s) },
{ e -> println(e) },
{ println("onComplete") }
)
// ...and here's an example using RxKotlin. The named arguments help
// to give your code a little more clarity
Observable.create<String> { emitter ->
emitter.onNext("Hello, World!")
emitter.onComplete()
}
.subscribeBy(
onNext = { s -> println(s) },
onError = { e -> println(e) },
onComplete = { println("onComplete") }
)
}
我希望有所帮助!
答案 1 :(得分:3)
查看RxKotlin,这将简化很多事情并使代码更简洁。
val list = listOf("Alpha", "Beta", "Gamma", "Delta", "Epsilon")
list.toObservable() // extension function for Iterables
.filter { it.length >= 5 }
.subscribeBy( // named arguments for lambda Subscribers
onNext = { println(it) },
onError = { it.printStackTrace() },
onComplete = { println("Done!") }
)
答案 2 :(得分:2)
val observer = object: Observer<Int> {
override fun onNext(t: Int) {
// Perform the value of `t`
}
override fun onComplete() {
// Perform something on complete
}
override fun onSubscribe(d: Disposable) {
// Disposable provided
}
override fun onError(e: Throwable) {
// Handling error
}
}