android-Kotlin Rx:必需的使用者,找到KFunction

android-Kotlin Rx:必需的使用者,找到KFunction,第1张

概述我正在使用KotlinRetrofitRx.我想将请求之一提取为函数:fungetDataAsync(onSuccess:Consumer<Data>,onError:Consumer<inThrowable>){ApiManager.instance.api.getData("some","parameters","here").sub

我正在使用Kotlin Retrofit Rx.我想将请求之一提取为函数:

fun getDataAsync(onSuccess: Consumer<Data>, one rror: Consumer<in Throwable>) {    apimanager.instance.API            .getData("some", "parameters", "here")            .subscribeOn(Schedulers.io())            .observeOn(AndroIDSchedulers.mainThread())            .subscribe(Consumer {                time = System.currentTimeMillis()                onSuccess.accept(it)            }, one rror)}fun onbuttonClick() {    getDataAsync(this::onSuccess, this::onError)}private fun onSuccess(data: Data) {}private fun one rror(throwable: Throwable) {}

我在getDataAsync(this :: onSuccess,this :: one rror)行中收到错误:

Type mismatch: inferred type is KFunction1<@Parametername Data, Unit> but Consumer<Data> was expectedType mismatch: inferred type is KFunction1<@Parametername Throwable, Unit> but Consumer<in Throwable> was expected

如何解决?

解决方法:

不用传递Consumer作为参数,您可以传递一个函数

fun getDataAsync(onSuccess: (Data) -> Unit, one rror: (Throwable) -> Unit) {     apimanager.instance.API        .getData("some", "parameters", "here")        .subscribeOn(Schedulers.io())        .observeOn(AndroIDSchedulers.mainThread())        .subscribe({            time = System.currentTimeMillis()            onSuccess(it)        }, one rror)}fun onbuttonClick() {   getDataAsync(this::onSuccess, this::onError)}private fun onSuccess(data: Data) {}private fun one rror(throwable: Throwable) {}
总结

以上是内存溢出为你收集整理的android-Kotlin Rx:必需的使用者,找到KFunction全部内容,希望文章能够帮你解决android-Kotlin Rx:必需的使用者,找到KFunction所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1081019.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存