Callback with parameters with Kotlin

user2434385 :

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.

Zoe :

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
    }})

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=461152&siteId=1