August 2017
Intermediate to advanced
440 pages
10h
English
If we need to do a long operation, and we don't want to make the user wait for it, then we have to start it in another thread. To be able to use a callback after a long operation called in a separate thread, we need to pass it as an argument. Here's an example function:
fun longOperationAsync(longOperation: ()->Unit, callback: ()->Unit) {
Thread({ // 1
longOperation() // 2
callback() // 3
}).start() // 4
} // Usage longOperationAsync( longOperation = { Thread.sleep(1000L) }, callback = { print("After second") } // 5, Prints: After second ) println("Now") // 6, Prints: Now
Read now
Unlock full access