https://kotlinlang.org logo
#android
Title
# android
k

kingsley.gomes

11/09/2017, 11:24 AM
Hi, I'm new to kotlin how can i return data from an anonymous function?
Copy code
val flowable = mWebService?.getData
            flowable?.subscribeOn(<http://io.reactivex.schedulers.Schedulers.io|io.reactivex.schedulers.Schedulers.io>())
                    ?.subscribe({ response ->
                        Logger.i(TAG, "onNext $response")
                        return true //<------------ how do I return this
                    }, { throwable ->
                        Logger.i(TAG, "onError $throwable")
                    }, {
                        Logger.i(TAG, "onCompleted")
                    })
g

gildor

11/09/2017, 12:03 PM
Just omit return, it's lambda and result of the last expression will be returned from lambda
m

marcinmoskala

11/09/2017, 12:05 PM
And it is lambda expression
g

gildor

11/09/2017, 12:12 PM
Also, all those nullable properties looks bad (a lot of question marks), better to avoid them if it's possible on this case. imho
r

radityagumay

11/10/2017, 1:13 PM
I am afraid you can't do that. As your code is async. So it will not directly return value. You can omit subscribe. Than call flowable.subscribe { isSuccess -> } unless you make RxJava is not async. Another options is using couroutine.