Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Completion Handler Android Kotlin

I´m sorry if this question has been asked previously, I really couldn´t find anything not even simillar! I´m as well sorry if the question is dumb, I´m an iOS Developer and I´m a bit lost here in Android...

So I´m using Fuel Library(https://github.com/kittinunf/Fuel) to GET JSON data from an API... In Swift there´s something called completion handler that whenever the function finished, it will return it and immediately run the code inside it. This is an example of it in Swift:

func hardProcessingWithString(input: String, completion: (result: String) -> Void) {
    ...
    completion("we finished!")
}

What I need is to do something similar with this following function that I have in Kotlin.

fun recomendationsData() {

    Fuel.get("https://rss.itunes.apple.com/api/v1/us/apple-music/hot-tracks/10/explicit.json").response { request, response, result ->
                    println(request)
                    println(response)
                    val (bytes, error) = result
                    if (bytes != null) {
                        val str = String(bytes)
                        val obj = JSONObject(str)
                        val resultsP = obj.getJSONObject("feed")
                        val results = resultsP.getJSONArray("results")

                        for (i in 0..(results.length() - 1)) {
                            val o = results.getJSONObject(i)
                            trackName1.add(o.getString("name"))
                            trackArtist1.add(o.getString("artistName"))
                            trackImage1.add(o.getString("artworkUrl100"))
                        }


                    }
                }
}

I´ve read about something called "callback" but I really don´t understand how it works, nor how to implement it(The task must be done Asynchronously).

Thank you very much again!

Regards

like image 481
Cristian Tabuyo Avatar asked Aug 30 '17 17:08

Cristian Tabuyo


People also ask

What is completion in Kotlin?

You can use Statement Completion to finish the current statement using ⌘⇧⏎ (macOS) or Ctrl+Shift+Return (Windows/Linux).

What are completion handlers?

A completion handler in Swift is a function that calls back when a task completes. This is why it is also called a callback function. A callback function is passed as an argument into another function. When this function completes running a task, it executes the callback function.

What is Handler Kotlin?

What is the use of handler class in Android? There are two main uses for a Handler: (1) to schedule messages and runnables to be executed at some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.


1 Answers

In this case the syntax is similar to swift:

fun recommendationsData(callback: (String) -> Unit) {

Then in your function you have a function called callback that you can call with the result (change String to whatever you're returning).

Then change your function invocation from recommendationsData() to either recommendationsData(doSomething) or

recommendationsData {
    doSomethingWith(it) // or you can get named argument
    // do some more stuff
}
like image 76
Brett Beatty Avatar answered Sep 21 '22 14:09

Brett Beatty