Only the latest version of Java has lambdas. You can also use a great library called Retrolambda to bring them into you Android App. Kotlin has lambdas built in. Learn about the nuances of working with them in your code as method pointers, finished blocks, and types—just as you would in Swift.
- [Instructor] One of the great language features of Swift is closures, and Kotlin has a similar concept called lambdas. A closure in most languages just means a lambda that captures some of the values that are outside of the code block. But in this, we'll just call these lambdas. I might refer to them as closures or blocks. In my mind, they're very similar pieces. So first, let's write a method that receives a lambda and that we can actually trigger the lambda within it.
We'll fake an asynchronous network call, asyncNetworkCall, and then we'll do a local one of these as well. And in here, I'll make this take a finished block and we'll have it return a Unit. Unit is Kotlin's version of void. And we'll have it receive a Boolean. Make sure that this is an arrow. So, the syntax for closures is really similar to Swift.
And I've also included a group of utilities to work with threading within Kotlin. It actually uses RxJava and RxKotlin, which is a little beyond this course, but this is probably the best way to work with any type of threading. So this is some code that you can use in any project that will help you get asynchronous processes working. So we have one that triggers async tasks and then the other one which dispatches to main. This always ensures that things are running on the main thread.
Android has similar issues as iOS when you try to update UI off of the main thread.0 So we're going to be using that threading library. I'll just say Threading.async task and here, we'll say the task is equal to this lambda. And in here, we'll just do a couple of print lines. And we'll say starting.
This is just so that we can see what's happening. In fact, we'll say... I'll say asyncNetworkCall starting and asyncNetworkCall finished. And then also in here, I'll just make the thread sleep for two seconds. And then we will trigger the finished block. I'll just say true, that it did finish successfully. Let's go ahead into the examples and we'll put in a few log statements in here.
I'll say println NetworkCall. We'll say this is going to be a LocalDBCall when we implement that code. And then we'll say this method, finished. So this will show us whether things are being executed asynchronously or not. For the NetworkCall, we'll just say Lambdas.asyncNetworkCall and it can receive a trailing closure because it's the last argument.
In the least, the concept, very similar in Swift. We'll just say message = if. Oh, and let's capture this variable. With Kotlin, you're not required to capture or use arguments coming into the closure unless you want to. In this case, we do, so we'll use the successful variable. Also within Kotlin, a lambda, you don't specifically state what its return type will be. You can only implicitly do that with closures.
So if it's successful, I'll say success. Else, failure. Unfortunately in Kotlin, we don't have the ternary operator. So instead, you can do an inline if-else block. We'll go ahead and just print what that message is. The next thing that we're going to do is that local database call.
So this one won't necessarily go create a new thread and should be called more quickly. So we'll just say Lambdas., we'll say a localDBCall. I'll just make a method here for right now. I want to say that it takes an authenticationBlock. This will just return a Boolean and we'll have it take in a string.
We'll do some local work, val userName. I'll just say that it's Naruto Uzumaki. We'll say isAuthorized is equal to the authenticationBlock with userName. And then we'll use that local result. I'll say that userName and isAuthorized is passed in.
And in here, we'll just do a simple print again. I'll say userName is authorized. Now you obviously would be doing real work here but we're just trying to show the asynchronous process of what's going on. So let's come back out here and now this is expecting a closure of some kind.
So in here, in fact, we'll just single line it. We'll say authenticate(it) and we'll just create this function. I'll just call this result. It will be a Boolean. In this case with Kotlin, we don't even need to have braces here. We can just make a one-liner. We'll say result ==. We'll just do a hardcoded string. It's Naruto Uzumaki.
We'll move this down into our helper methods region. And now, when we run this example, we should be able to see this asynchronous process going. Come down to the Android Monitor and look at System.out. So we can see that the NetworkCall happens, the local database call happens. We've done the local database stuff, so we know that the user is authenticated. And then the finished of this block happens. And then we begin starting our asynchronous process.
We finish it and then we call our final closure. Working with lambdas is very much like working with lambdas in Swift.
- Creating an Android emulator
- Similarities and differences between Kotlin and Swift
- Lambdas and exceptions
- Working with external extensions
- Extension collisions
- Using Java classes within Kotlin
- Working with network calls using Retrofit
- Writing a method to GET from the server
- Threading with AsyncTask
- Threading with RxJava
- Common UI components between Apple and Android
- Adding UI events
- Creating an activity