First, we’ll take away all of the auxiliary code to measure the execution time of the code and output the log. We don’t want this anymore. Now our coroutine will seem like the instance beneath. Let me remind you that the receiveData() serve as is suspended and can go back the outcome, which we’ll write to a variable: val information. Then we’ll use this information as a toast message.
The code is moderately easy, which is excellent as a result of it is going to assist you to higher perceive the subject, particularly in case you are taking your first steps on this course.
Now we’ll glance on decompiled Kotlin Bytecode. I in particular got rid of all of the code and left best the purposes which can be known as in it.
The invoke serve as might be known as first. It’ll be introduced by means of the CoroutineStart magnificence within the release serve as.
The invoke serve as takes the droop serve as and an example of the Continuation magnificence as enter parameters. We’ll speak about this magnificence in additional element, however for now, here’s what you want to grasp: there are two items that might be used when working code within the coroutine.
We want to return and have a look at the decompiled Kotlin Bytecode for the invoke serve as. There, we will be able to in finding those two items. If we examine, we get:
- Object var1 is block: droop() -> T
- Object var2 is of completion: Continuation<T>
As we take note, a complete of 3 purposes have been known as within the coroutine, and we checked out considered one of them. In flip, the rest two purposes might be known as within the invoke serve as: create and invokeSuspend.
A more in-depth glance presentations that the create serve as paperwork an object of the Continuation magnificence.
Additionally, this serve as is contained within the base magnificence Continuation. And no longer best this, however, as we’ll see, the invokeSuspend serve as, which we’ll have a look at just a little later.
And in any case, the 3rd serve as that might be known as when the Continuation magnificence object is created is invokeSuspend. It comprises all of the code that the coroutine will have to execute. As we already understood, this serve as might be known as by means of an object of the Continuation magnificence when the coroutine begins.
The primary objective of the Continuation magnificence is to be sure that the consumer interface operation (in our case, appearing toast) is carried out best after receiving the results of the asynchronous operation of our receiveData serve as. To try this, the code within the invokeSuspend serve as might be cut up into portions, and a label variable might be added to modify between items of code.
There are two items within: Object var3 and Object var10000. In Object var10000 might be put the results of the suspended serve as paintings, and Object var3 comprises a distinct consistent: COROUTINE_SUSPENDED.
The purpose the place the code splits into two portions is the droop serve as. It and all of the code prior to it is going to cross into the primary phase. And all of the code after it’s in the second one. Now, when calling the invokeSuspend serve as, the label variable will resolve which a part of the code might be performed. If label is 0, then the primary a part of the code (our receiveData serve as) might be performed, and if label is 1, then the second one a part of the code might be performed, the place the results of the paintings might be written to the variable var10000 after which used for show in toast.
As we discovered, the invokeSuspend serve as might be known as for the primary time initially of the coroutine and can execute the primary a part of the code in case = 0, which can release the droop serve as, and the invokeSuspend serve as will entire (go back).
Sooner than calling the droop serve as, a brand new price of one might be written to the label. That is essential in order that when the result’s gained, the second one block of code from case 1 will get started operating.
However a logical query arises: how will the serve as be known as if it has finished its paintings?
Notice that the receiveData serve as accepts this as a parameter. This is, the article of the Continuation magnificence itself is handed to the droop serve as.
When the droop serve as finishes its paintings, it is going to take the Continuation object that used to be handed to it and get in touch with its invokeSuspend serve as. For the reason that price of label used to be in the past changed by means of 1, when invokeSuspend begins working once more, the second one a part of the code from case 1 might be performed.
If truth be told, best two effects can come from the droop serve as.
- The primary one is the go back of the COROUTINE_SUSPENDED consistent that used to be discussed prior to. It implies that the droop serve as has no longer finished its paintings. On this case, the invokeSuspend serve as might be finished once more (go back). This may occasionally proceed till the second one imaginable choice comes from the droop serve as.
- The second is that if some price rather then the COROUTINE_SUSPENDED consistent is returned. This would be the results of the droop serve as. The code will proceed.
In case 1, the results of the droop serve as might be written to the var10000 variable, after which the code will proceed its execution. This variable, as we see within the code beneath, is used to show the toast, and then the serve as will entire its paintings.