代码如下:
getUsersObservable()
.subscribeOn(Schedulers.io())
//.observeOn(AndroidSchedulers.mainThread())
.map { user: User ->
user.email = String.format("%s@rxjava.wtf", user.name)
user.name = user.name!!.toUpperCase()
}
.subscribe(object : Observer<User> {
override fun onComplete() {
}
override fun onSubscribe(d: Disposable) {
}
override fun onNext(t: User) {
}
override fun onError(e: Throwable) {
}
})
我收到以下错误:
/Users/johann/Work/Dev/Kotlin/rxjava-walkthrough-kotlin/app/src/test/java/ktplay/RxKotlinPlay.kt: (49,14):以下功能均不能使用 提供的参数:@CheckReturnValue @SchedulerSupport public final fun subscription(p0:(((Unit)-> Unit)!):一次性!定义于 io.reactivex.Observable @SchedulerSupport公共最终乐趣 subscription(p0:观察者):在中定义的单位 io.reactivex.Observable @CheckReturnValue @SchedulerSupport公共 最终乐趣Subscribe(p0:Consumer!):一次性!定义于 io.reactivex.Observable
在订阅的行上发生此错误。
答案 0 :(得分:2)
当前,您的map
运算符采用User
并返回Unit
而不是User
。它应该看起来像这样:
.map { user: User ->
user.email = String.format("%s@rxjava.wtf", user.name)
user.name = user.name!!.toUpperCase()
user
}
答案 1 :(得分:0)
仅修改为“安全”订阅观察者(用户?>
.subscribe(object: Observer<User?> {
override fun onNext(t: User) {
// 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
}
})