Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback with parameters with Kotlin

I just started Kotlin so please be nice :)

I have a class that is responsible for fetching some data and notify the main activity that its need to update its UI.

So i have made a function in my DataProvider.kt :

      fun getPeople(fromNetwork: Boolean, results: ((persons: Array<Person>, error: MyError?) -> Unit)) {

        // do some stuff stuff
        val map = hashMapOf(
                "John" to "Doe",
                "Jane" to "Smith"
        )

        var p = Person(map)
        val persons: Array <Person> = arrayOf (p)
        results(persons, null)
    }

So i want to call this from my activity but i can't find the right syntax ! :

    DataProvider.getPeople(
            true,
            results =
    )

I have try many things but i just want to get my array of persons and my optional error so i can update the UI.

The goal is to perform async code in my data provider so my activity can wait for it.

Any ideas ? Thank you very much for any help.

like image 763
user2434385 Avatar asked Apr 19 '18 17:04

user2434385


People also ask

How do you pass parameters in Kotlin?

In Kotlin, You can pass a variable number of arguments to a function by declaring the function with a vararg parameter. a vararg parameter of type T is internally represented as an array of type T ( Array<T> ) inside the function body.

How do you do a callback on Kotlin?

Callback Block You can define the structure of the getTitle function as a block directly at the invocation of the buildTitle function. Then, all you have to do is indicate the parameter that it will receive and add the code to execute.

What is a callback parameter?

Any function that is passed as an argument is called a callback function. a callback function is a function that is passed to another function (let's call this other function otherFunction ) as a parameter, and the callback function is called (or executed) inside the otherFunction .

What is .USE in Kotlin?

} use accepts a function literal and is defined as an extension on an instance of closeable. It will close down the resource, just like the try-with-resources construct, after the function has been completed, whether an exception was raised or not.


1 Answers

This really depends on how you define the callback method. If you use a standalone function, use the :: operator. First (of course), I should explain the syntax:

(//these parenthesis are technically not necessary
(persons: Array<Person>, error: MyError?)//defines input arguments: an Array of Person and a nullable MyError
     -> Unit//defines the return type: Unit is the equivalent of void in Java (meaning no return type)
)

So the method is defined as:

fun callback(persons: Array<CustomObject>, error: Exception?){
    //Do whatever
}

And you call it like:

DataProvider.getPeople(
    true,
    results = this::callback
)

However, if you use anonymous callback functions, it's slightly different. This uses lambda as well:

getPeople(true, results={/*bracket defines a function. `persons, error` are the input arguments*/persons, error ->  {
        //do whatever
    }})
like image 142
Zoe stands with Ukraine Avatar answered Sep 27 '22 17:09

Zoe stands with Ukraine