Understand Kotlin Coroutines on Android (Google I/O'19)

[Music] my name is Igor I'm an engineer in the Android team hey my name is Sergey and I work in the same team hi I'm Sean I'm from Android dev rel okay today we are going to talk about qualities on androids but before we talk about that let's try to

figure out like why do you even need some qualities and to understand that let's get how you write UI code on Android so this is the dream code we want to write you like color function makes a network request whatever you get the user set it on the text view this is what you want to write but if you write that you get an exception because you cannot make a network request on the main thread easy we just put an insider threat and run the code now you're going to complain is the text view which is like you cannot touch the UI thread from a background thread and okay go write this kind of code where you make it a synchronous you provide a callback it runs on the background thread and cause your callback on the UI thread and this code works fine except if you're writing code like this you're gonna receive out of out of memory exceptions because you're going to be leaking those callbacks left and right there's a solution to this as well where we can like have an understanding of a subscription that keeps his chain and whenever you're a stop we can just cancel the subscription that one works but then you end up something like this and I'm not making this up by the way three years ago when we started architecture components I was looking at the Google app code and I found one application that had 26 lines of amount math unregister so if it happens at Google it happens everywhere like we'd always write the base code but there's a solution to this as well which if you are using something like Eric's Java and so this function returns an observable you can just use the auto dispose library associated subscription with your real life cycle and safely subscribe and this works perfectly similarly you could be using live data which enforces you to have a life cycle to observe and this also works so this whole problem right why are we even talking here about this if there's already good solutions every year we run these developer benchmark service we ask the voters how are they doing but are there problems and the service we run last year one of the top cop plays was threading and concurrency developer says this is hard on Android and one of the top Lucas was this week what we call live data plus once the people want us to extend live data make it more like Eric's Chara and you're like why why do you want this we have good solutions just use one of them so we didn't bother we do best when we don't know we did a UX research this is a user experience research on concurrency so we did in-depth interviews with nine developers what they do is they do the regular work for a couple of weeks in their own company and every time they see a problem about concurrency like this observable T they just write it down this was the problem this is I sold it and this is how I feel about it and in this study we focused on three main things focused on live data which is our observable date older we focused on Eric Shaw with the reactive Sims library and qualities which provides suspend of all computations and in the result of that study this was the conclusion for live data people say we love it but we want a complete solution in fact is funny live data doesn't even support anything but a main thread but we talked about it in a concurrent study for our Xterra it is amazing people love and hate it they love how powerful it is but the common complaint we always heard was well as always misused it feels like an overkill and for qualities this was like it really looks like the best solution but I'm not sure it's very new it's not major so this was the overall conclusion we said we need a solution that is simple it shouldn't be hard to learn that solution it should be compressive so you should be able to scale to different use cases and it should be robots that should be built in testing stories so made two decisions we said okay we are going to have first-class quality support in jetpack and we are going to have more support for rxjava in our documentation but today it is all about Cortese so shut why don't you tell us a little bit more about what qualities are thanks you so I want to just take five minutes and talk a little bit about what problem carotenes are great at solving so in a sentence the main problem the correcting solve is simplifying async programming by replacing callbacks which is quite abstract so let's look at some samples and see what that looks like I'm gonna make an imaginary Network request three ways the first style is what's called a blocking style this is where the result is returned directly from the function let's see how that executes and for fun I'm gonna run that on the main thread when called a blocking network Hall will block the thread that called it so the entire time that Network request is running the main thread will be blocked and that's the thread that has to update the UI and handle user touches so the user will see your app is frozen or it might even crash now I do want to pause and say there's nothing wrong with a blocking style of network api's but it's not what we want to do on android so to fix that as he'd already talked about we commonly introduce callbacks so let's see how bad executes we're still gonna call this from the main thread but now when fetch user is called the main thread is free to perform other work it can handle on draw or respond to user touches and the networking library is responsible for finding another thread to actually run the request when the result is ready the network library can then use with a callback I gave it to callback into my code and let me know that it's ready let's rewrite that exact same code with co-routines it looks just like the blocking style the result of fetch user is available immediately and I don't have to introduce a callback to tell Collin I want to keep this with co-routines it has a suspend modifier on the function and when we run it still on the main thread the main thread is unblocked just like with callbacks and this is a key concept of carotenes the networking request still runs on another thread when the result is ready it resumes the curry team where it left off this code is much simpler than the callback style while still ensuring that I can write my Android app and make it never freeze for the user this is the key mechanism here of correcting suspend and resume when a KO routine is suspended it's not running it's paused and when it resumes it picks up from where it left off you can think of suspending ko routine as taking a callback from the rest of the function so you've put it together suspend and resume replaced callbacks we can even visualize that the callback version and the co-routine version execute almost exactly the same way let's switch back and take a look at fetch user how can we call a function that makes a network request from the main thread to start we'll need to make fetch user another suspending function this tells Kotlin that it works with care routines and inside will call another suspending function called with context well pass it dispatchers done I am zooming in on those dispatchers calling gives us three dispatchers default IO and main and they're used for different things default should be used for CPU intensive work things like transforming a list of a hundred elements calling des futile or pre computing text anything that takes too long to run on the main thread should run on the default dispatcher IO is a dispatcher that's optimized for blocking Network in disk IO you should use it anytime you need to write code that blocks an API like writing a file or reading from a socket and main this is the main thread on Android and surprisingly it's our recommendation as the right place to start co-routines in response to UI events since you're usually starting para teens from the main thread staying there will avoid extra work for simple operations then when you need to transform a list or write a file co-routines let you switch to one of the other dispatchers by using with context with context will run the block that you pass it on the dispatcher you tell it to so this block here is gonna run on dispatchers dot IO and I'm free to make blocking Network calls this allows us to provide main safe api's you can just make a function that reads and writes from the network like this and call it from the main thread and this is a huge benefit on Android now I don't have to worry about what every single function what thread it needs to run on instead I can just call it and the function itself can ensure that it's safe to be called from the main thread to finish up introducing pair routines let's take a look at how Kotlin implements them every thread has a call stack it's what you see in the debugger or a stack trace it's how collin keeps track of which function is running and its local variables when you call us to spend function Collin needs to keep track of the fact it's running a KO routine instead of a regular function I'm gonna represent this as a suspend marker everything above the suspend marker will be a couraging and everything below will be a regular function then Kotlin calls load user just like a normal function it's gonna put a stack entry onto the call stack and this is where any local variables for load user would be stored and then it just executes until it finds another suspend function call now Kotlin has to implement suspend how does it do that what's kind of simple once you figure it out all Kotlin has to do is copy the state of the function from the stack to a place where it can save it for later it'll put all suspended coverage scenes out here and it's not structured like a stack then Collin will actually call fetch user creates another stack entry and when it calls with context suspends that as well so at this point so at this point all of the curry teams on the main thread are suspended and this means the main thread is free to do other work like handle on draw or respond to user touches and this is really really important when all of the curry teens on a thread are suspended the thread is free to do other work if we fast forward a few seconds the network result will be ready and Colin will have to call resume in order to do that it just takes the save state and copies it backs over puts it on the stack and resumes the function when it resumes load user it'll just go ahead and continue executing just like normal load user head aired it would have thrown an exception right there the suspend and resume mechanism is the magic behind co-routines and we wanted to show it to you so you could understand how they work as you start using them in your code that wraps up the core routines intro hurry teams on android offer us the ability to simplify our code by replacing callbacks and allow us the ability to create main safety to ensure we never block the main thread now I'm going to hand it over to Sergey who will talk a bit about libraries you can use today with heritance thanks John yeah those very cool bits on ice but we really want to benefit in our real application from it and despite a very young age of curtains there are libraries that already support them in very stable orbit artifacts and I want to start with work manager that is Payoneer for us in Android X because it's already supports car scenes in its stable release and you can use this curtain worker but let's make a step back and try to figure out why we do that we use it so this is a typical flow of workers so if you are not familiar of work manager you can think of worker just something that does long pink round job it may have some constraints but it's very simple just some work and typical use case for that you need to synchronize some local data with your web server and this flow would look like you query your new notes from your database then upload it to the web server lastly you just mark those notes is successfully singing well you see no need for curtsies well actually we didn't start to talk about translation because translation may happen tea variety reasons for example constraints for this worker aren't met anymore or user explicitly cancel this job if you provided this UI so how you would support cancellation well you can try to do something like that you try to put like every other line this if check and it starts to look silly and even more it doesn't actually work because this call which is probably most expensive call because it goes to the network and do some work there it doesn't have any consolation signal propagation because well if it was started it will run till its end no matter what and this actually will curtail worker will help us with that we didn't talk about that yet but curtains don't really don't only ruff callbacks nicely it also provides nice consolation property so every system function can be cancelled it can react on this cancellation and also it propagates to all inner calls this cancellation signal well you may say our code inside those calls are still blocking we don't benefit from that anyhow this is true however if you use room as your database solution you can mark your queries and suspense functions and then room will take care of consolation for you as well as fretting as Sean mentioned that in multiple times this thing will be main safe room takes care of reading it will run the query on a background thread then well nice our database calls are canceled now but as we discussed before the main call is this one and actually if you use retrofit you can make it suspend as well because retrofit already for suspend modifier for its Network calls and I want to highlight that a retrofit isn't part of Android X it's a just job that's done via a community Android Android a cotton community embraces college scenes and we like it at the end of a day it's less work for us so nice now this code supports constellation and it looks as as easy as it looked before so we got a constellation for free so this was a quick look on the things that were available today and it will present you a lot of new price that we just made so so far we talked about what you could do with qualities and for the rest part of this talk we are going to talk about new stuff so first one is live data and qualities now just to be very clear live data is not designed for concurrency it's an observable value holder and you are expected to be able to access the value from the main thread that's like intentional but that doesn't mean it should not be interoperable so this is what we are going to provide you today there will be easy way to use live data with Cortese so the most common use case is you have some value you want to compute in a quarantine but smart to serve the result is a live data so to turn starting today with the lifecycles 2.

2 of a one artifact you get this new one new API called live data so it's a builder function very similar to the sickness wielders in Cortland inside that you pass a quality block and he said you could do whatever you want and call this amid function to dispatch values so if you look at this data with load function it is HLS husband function and because you are calling the image with a user in this case we can infer that type for you so you don't even need to specify this so this very simple light that the API bridges the gap between your live data elements and your qualities so you get that ap a little bit more in detail so it receives three parameters and the first one is a contact so why do we need a context well if this data is loaded user function is not less than the sass file function which is a regular function and you write this code you are going to receive an eye on main thread exception because this vlog by default resume dispatchers main but you can change that we can tell it give it a context as dispatchers IO and now this code will work perfectly I want you to notice that I didn't change any contents of the code because you can omit from whatever dispatcher you want you don't need to be on the main dispatcher to change the values now the second one is a really awkward parameter called timeout and to understand why we needed a timeout parameter let's look at the a famous rotation problem on Android so on the Left I have a view model that serves a live data and on the right I have an activity that's observing it so when my activity goes to started state the live data will become active which means ok you're an observer visible to the user you are better off creating some values but during that time what if our activity rotates and so it's going to be stopped my data will become inactive be destroyed and a new activity will come so right now there is no one observing live data so there's no reason to produce results except after the new one goes started again it becomes active again so the problem we are trying to solve here is this gap where live data quickly becomes inactive and active in a very quick succession like usual less than one second so how do we fix that let's look at the detail how we run that code block and to understand it better we're just going to write a timer function it's basically creates a timer for live data it gets the current time returns a live data builder and in an infinite loop it just missed the time the last one second a miss the time delays one second and never ends and this code I'm showing is hundred percent okay to write how does it actually work when the live data returned by this blog becomes active we check okay did we run this block and if we didn't run that block now we start executing it while we're executing it if that block becomes inactive like if the live data becomes inactive we check okay is this block still running and if it is still running we give it some time to finish but even after the timeout if it is still running and we are inactive that's basically unnecessary computation there is no one observing the live data but the block keeps running so we just cancel the continuation the recording so if the like that' becomes active again we're just going to restart it and we only do it once so if it finished the completion there is no reason to restart it now you can also emit more than one value so this was some

https://edgarqvzt.bloggersdelight.dk/2020/03/29/best-kodi-addon-2018-for-movies-tv-shows/
https://5e80b8910c5ea.site123.me/#section-5e80b959e5d8d
https://www.storeboard.com/blogs/general/best-kodi-addon-2018-for-movies-tv-shows/2232324
https://augustmcxy596.skyrock.com/3331338092-Best-Kodi-Addon-2018-For-Movies-TV-Shows.html
http://andresfgpx271.iamarrows.com/best-kodi-addon-2018-for-movies-tv-shows
http://chancernfh155.jigsy.com/entries/general/best-kodi-addon-2018-for-movies-tv-shows
http://judahfhzx584.yousher.com/best-kodi-addon-2018-for-movies-tv-shows
https://b3.zcubes.com/v.aspx?mid=3464799&title=best-kodi-addon-2018-for-movies-tv-shows
http://andersonrpvl780.huicopper.com/best-kodi-addon-2018-for-movies-tv-shows
http://e6bttpl260.nation2.com/best-kodi-addon-2018-for-movies-tv-shows
http://the-master-blog-6474.223353.n8.nabble.com/best-kodi-addon-2018-for-movies-tv-shows-tp2.html
http://andresljsu595.nikehyperchasesp.com/best-kodi-addon-2018-for-movies-tv-shows
http://troyyair811.canalblog.com/archives/2020/03/29/38144996.html
http://rowanltgq683.unblog.fr/2020/03/29/best-kodi-addon-2018-for-movies-tv-shows/
http://johnathanvsud698.institutoalvorada.org/best-kodi-addon-2018-for-movies-tv-shows
http://waylonnfch506.fotosdefrases.com/best-kodi-addon-2018-for-movies-tv-shows
http://beauqcnu119.zoninrewards.com/best-kodi-addon-2018-for-movies-tv-shows
http://messiahyhpc318.image-perth.org/best-kodi-addon-2018-for-movies-tv-shows
http://edgarlxor873.lowescouponn.com/best-kodi-addon-2018-for-movies-tv-shows
http://remingtonlqbi945.lucialpiazzale.com/best-kodi-addon-2018-for-movies-tv-shows
http://lorenzorunr590.bearsfanteamshop.com/best-kodi-addon-2018-for-movies-tv-shows
http://claytonbjiu362.cavandoragh.org/best-kodi-addon-2018-for-movies-tv-shows
http://remingtonakjg375.raidersfanteamshop.com/best-kodi-addon-2018-for-movies-tv-shows
http://andersonjrna917.tearosediner.net/best-kodi-addon-2018-for-movies-tv-shows
http://mariozcog039.theglensecret.com/best-kodi-addon-2018-for-movies-tv-shows
http://simonlcoe567.timeforchangecounselling.com/best-kodi-addon-2018-for-movies-tv-shows
http://paxtonkiet679.trexgame.net/best-kodi-addon-2018-for-movies-tv-shows
http://emilianoiran993.westbluestudio.com/best-kodi-addon-2018-for-movies-tv-shows
http://alexishtya067.almoheet-travel.com/best-kodi-addon-2018-for-movies-tv-shows
http://holdenigup439.theburnward.com/best-kodi-addon-2018-for-movies-tv-shows
https://d4utely078.wixsite.com/dallasmdbq918/post/amazon-fire-tv-stick-4k-with-alexa-voice-remote-and-vouc
https://zanerecd621.hatenablog.com/entry/2020/03/29/220540
https://www.liveinternet.ru/users/d9xijwp978/post468580634//
https://elliotthsku962.wordpress.com/2020/03/29/amazon-fire-tv-stick-4k-with-alexa-voice-remote-and-vouc/
https://www.evernote.com/shard/s481/sh/7f918c21-737b-d140-1aed-0be6b173b126/ab3ca11680cdefe3c2edabf6022d1e7b
https://diigo.com/0h5aww
http://edgarccho649.simplesite.com/445529645
https://telegra.ph/amazon-fire-tv-stick-4k-with-alexa-voice-remote-and-vouc-03-29
https://marcoczer464.tumblr.com/post/613907517712564224/amazon-fire-tv-stick-4k-with-alexa-voice-remote
http://judahgcjn582.bravesites.com/entries/general/amazon-fire-tv-stick-4k-with-alexa-voice-remote-and-vouc
https://www.smore.com/8cure-amazon-fire-tv-stick-4k-with-alexa
https://andersonnyoq486.edublogs.org/2020/03/29/amazon-fire-tv-stick-4k-with-alexa-voice-remote-and-vouc/
http://waylondnnp814.xtgem.com/amazon%20fire%20tv%20stick%204k%20with%20alexa%20voice%20remote%20and%20vouc
http://daltoniaid634.wpsuo.com/amazon-fire-tv-stick-4k-with-alexa-voice-remote-and-vouc
https://penzu.com/p/e31ac2bd
https://stephenzpgn.bloggersdelight.dk/2020/03/29/amazon-fire-tv-stick-4k-with-alexa-voice-remote-and-vouc/
https://5e806a078975f.site123.me/#section-5e806ad16c6fc

Nhận xét

Repost

BestBuyIPTV Play IPTV with MAC Address