 Everyone, thank you for coming So unfortunately the speaker that was supposed to give this presentation is sick and Is some of these apologies, but it's unable to be on stage The talk we've been experimenting with a codling compiler and that's really unfortunate, but we found a Volunteer that took over the slot. So we're going to have Jeffrey on stage talking about Modern a synchronism with curtains Good morning everyone So I just present the coroutines the most generic way I could because I am an Android developer too I do a second talk this afternoon at 1 p.m. Which will be more specific to Android So I won't cover everything for Android right now. You can come this afternoon for almost fill it up first of all represent myself I work at Video labs, which is a company founded by video land people to have people full-time on VLC. So I work on the Android port of VLC So I'm sorry short code coroutines are just The codling compiler wrapping the callbacks for you. So Do I have to speak louder or is a mic Okay, is it better? Yes so so Coroutine that then just under the root wrapping callbacks for you. So the compiler will You will write line your code sequential calls and then the compiler can do some callbacks For you and you oh, you don't have the callback you usual callback helps when you do some asynchronism So you don't need to look and you don't need to write callbacks And you less need to lock to block the the current thread to you won't use mutex anymore Simaphones So the parody now now is suspending Instead of blocking a thread. So we have a suspending function which frees the current thread Which is available for the task and then we'll call back later So the entry point of coroutines are is the launch call usually it returns a job and the job is the Controller of the your coroutine You can specify this patcher which will decide on which thread will the coroutine executed The the second most useful the with context so inside a coroutine this will suspend so this coroutine and Execute the the inner code in the context you specified here I chose to use dispatches.io which is a IO dedicated thread pull to do background IO operation which will be blocking so During this time the main thread is free You can just continue to display your app you can still have inputs from user while the the file is being fetched And once the file is ready in this coroutine. We come back to the main thread when it's available and we continue our task There is the async call which mimics the async await from t-shop Which is more suited if it works a bit like with contests, but it's more suited for parallel calls you need So here we can call two async Task the two different async task and then await for both of them So this way we'll have two parallel job launched and what's there one there are both ready We can just leverage the result This in Java would be Complicated to achieve that will be a lot of callback else actually and this becomes really really simple with the coroutines So here the catch is the dispatching the You see the cause simple the code is way more simpler, but under the hood you have to get in mind that The The execution is not a call stack anymore like in a regular function So it's not you don't know the exact state of your thread Potentially typically when you start from the main thread you do a with one text Background task when you come back to the main thread everything can happen Maybe the view has been just wiped in the meantime and the task is not valid anymore So this is the trick we have to know with coating it's simplified the code But under the hood we have to know that like with regular threading We don't know the state of the calling thread the ordinary original one We can also specify when we launch according to not dispatch Immediately because by default we just post the call to the main thread which will be executed later so we leave the current function and that's it so far the Android developer the The main dispatch the dispatches dot main implementation is based on the handler. It's just simply posting a Renable to a handler When you will we launch a routine I showed you we chose a dispatcher, but actually we can specify more complete context and context will be with the most important element are the dispatcher and parent job and We can also specify exception under and name so dispatcher will just decide where the code will execute Job is an interesting Feature that it will twill be a controller on your coroutines So we can join this job like we join a thread in the C++ for example And we can also cancel it and the coroutines can have a parent job That's what we specify when we launch it and if you cancel a parent job all the Children coroutine will be cancelled to and that's really powerful to cancel operation when a specific scope is not valued anymore That's why gel paints implemented the Coroutine scope interface This is basically just an order for coroutine context, but it's it will Create the pattern of always specifying a coroutine context and machining to a life cycle so when you have Session you can Define the scope the coroutine scope matching this session and when the session is over you can sell your coroutine scope Which will in fact cancel the parent job you Affected for every coroutine so all of you barrow ground and waiting tasks will be automatically cancelled when you decided This is not this is no longer no longer valid. There is a global scope, which is Not deprecated, but it's not advised to use it, but this is an on full for From migration from regular Java code to Kotlin So this is how we match Coroutine scope to a life cycle. This is another review model Which has it's life cycle is it is creating and when the corresponding view is definitely destroyed It will be cleared. So here we just define a parent job The dispatcher we want to use so it this is the main one and then on clear we just You we Can't sell the job and we can call the scope that can sell a call which is just a shortcut to this I just a word regular job, but there is a variant Supervisor job and supervisor job will not cancel when one of its children of a child fails the former example with the two parallel parallel async calls With a regular job if one of these call Fails the entire scope is cancelled This is convenient when you really need the the two results for example You don't care about waiting for the second one when one has already failed in most Main thread tasks you want to use a supervisor job because if one task fails you just you don't want to and Breptly stop the user experience So on some notes Coroutine are very powerful, but it breaks the interval interoperability between Java and Kotlin There is some a text class Which works really like the normal one, but it doesn't block it just suspends the it and it has to be executed So in a correcting context there is a volatile and annotation in Kotlin, which is quite useful too Just matches the volatile the Java volatile and That's it and So now I'll just show you some Some way to use these coroutines who do some really cool stuff and more complicated one in a simpler way The first one I'll show it the callback limitation will Will wrap a multiple shot callback in a coroutine So in this example, I took the the libvlc browser API, which is an NC conus browsing API. So we just Tell we want to browse this folder and then we have callbacks for every media discovered and one callback once the Discover is over so in practice we have to Define listener which will receive the callback then and So for refresh we have to start this discovering and the listener will continue these executions So we have the function occurring in two different places So in Kotlin, we have channels Go developer should know this. It's really convenient We can just post events and and wait for receive them and here. This is what I use I just defined a channel the The callbacks We'll just post on it and then when we want to refresh we will use this channel will initiate it and use it to just Suspend we start the Request browsing then we the the for loop is it's a select actually So this for the select will just suspend the function So the current thread is suspect. It's free Function is suspended and every time we receive a media from the background or background task. It will just Execute and we can add our media in the main thread right when they they pop and Then we can update or that I set it the that I meant for the QI Then it brings some nice addition We have some operators like a map if we need some transformation when we have our raw media Before sending into DUI We can also wait to have the full list. So we'll wait for the The browse and being called for the list to be ready and once we have that we are we have the full list ready Channel has been that's an experimental API. So this is prediction ready, but this API may move like the actors Actors is basically a simple wrapper on top of channel The first example is for adapter The where working is basically we post the list of elements To it and it and it calculates the diff exactly like gith does between those two to update the view so We we have to send the update function will just receive the new list We send this list to an actor which is under the channel In this way, I chose a conflated one this way if we send like 10 list at once The conflated will just ignore the intermediary step. So while the first one is being processed The nine other arrive on this and every new one will just override the former So in the end we'll just just process the first and the last one For you I we don't care about just updating that much steps. We just want to be up to date And then So in this channel, we are in the scope. So we are kind of suspending function. So in this spending function I call I do the calculation in the default thread pool, which is sweat pool with as much thread that you have of CPU cores and Then we come back to the main thread to post events and then we can process the next one in the list So it's a queue of events which can be suspended Another way of using actors. It's to confine mutability That's what we do every day with the UI thread or main thread So typically views cannot be modified outside of this main thread because of possible concurrent access So the mutability of the UI is confined to the main thread. We can do this with an actor. So we can find it in this in this specific Coroutine So as this is a queue it guarantees we cannot have Multiple access at the same time. So here I can do like deletion modification and read access Safely, this is just cute. It's not immediate, but This is guaranteed to to not be concurrent. So I don't block anything. I don't need to use a matrix. I Just confine it in it in this actor So here I just use an unlimited capacity for example to not conflate and not ignore elements and Then I just have to post elements to this Another example of Matching in the scope with the life cycle. This is the view life cycle on 100. I Write what I showed you Sooner I I use Delegation for our implementation on 100. So I implant implement the curtain scope by with the main scope Function and I just now have to call the cancel in the on destroy and this Makes me allows me to use coroutines in this view and All of the curtain calls will be cancelled when the view is over We can play with it and add some useful Extention function like for any view. I can just use this is our other function We just post the action one to do we use an actor to stack the events to be processed So this is a listener the listener will post to the actor This is automatic and then all of this will be are you understood and automatically come sold well when we don't need this view anymore I show now another way to wrap callbacks, but single shot callbacks So we just want one single event from a call. I Took a rest call for example. We just send a HTTP request and one want one result result So this is our retrofit used to work and now they natively support support coroutines so we had to To make a call and we had callbacks for the the result We can directly you create a coating like this with the with the suspend curtain come the Suspend can several coroutine This wheel just will create our callback in it which will continue the coating with the correct result We are the important elements so we call suspend curtain the call on Q is the call The continuation is the state machine holding the coating states So we either with you meet with the results we add and then this function will return the result with the correct type directly Or we failed and we have with them reason exception and we can just try catch this call or use a coroutine exception on there for example So usage becomes pretty becomes pretty simple. We just have to wrap our API calls in this higher order function and we get suspending function returning directly the The great up now from this is confirmed. I want to browse a path. So I just receive a list of files And we don't care about this Network logic anymore. We don't care about threading or whatever. We just have to be in a coroutine context We call it the Coroutine will suspend the main thread will be free from the time being and then once it's ready. We have our data My second example of the very same application in VLC. We have like this is an Android TV. We have channels on on screen so we May have to start media with and before the LC being actually launched So media library is not ready. I did the same implementation with a shortcut if If the media library library is already ready. We don't care. We just execute the call otherwise, we do just what I showed you we Will start the launch a start call for the code for the media library and this coating will just suspend one The time for the library to being read to be ready And then we we resume and we execute the call we wanted to do on the mail media library this become This becomes really really simple to use It's been a very very complicated case Before coatings and this is now a single line of code to just get a media whether the media library is ready or not So thank you. I want to know a bit more and more specific to Android I invite you to come back at 1 p.m. And I'll present you the flow IPA which is bright new too Otherwise, there are some links if you want to discover a bit more and thank you for listening Are there questions? What can we get your slide? What can we get your slide? Can we get this right? Okay, I I just had to prepare it really I post the slide URL on the on the first them Speak also you have it in the app and on the website anyone else. Thank you very much