Android Coroutines: How to manage async tasks in Kotlin

  Рет қаралды 41,761

Android Developers

Android Developers

Күн бұрын

Take a good look at the current state of coroutines support in Android libraries and tools, explore some interesting and not-so-obvious edge cases when dealing with Kotlin code that includes coroutines and interop with Java, and briefly look at some new developments in Kotlin coroutines, namely StateFlow and its comparison with LiveData, and SharedFlow.
Resources:
Coroutines blog posts → goo.gle/coroutines-posts
Coroutines 101 codelab → goo.gle/coroutines-101
Other coroutines codelabs → goo.gle/3fo7B6R
Check out the 11 Weeks of Android website → goo.gle/30FDT8S
Related Playlists:
Languages - 11 Weeks of Android playlist → goo.gle/languages-11weeks
11 Weeks of Android playlist → goo.gle/android-11weeks
Subscribe to Android Developers → goo.gle/AndroidDevs
Speaker:
Manuel Vicente Vivo
#featured #android11 #11WeeksOfAndroid

Пікірлер: 65
@goobar
@goobar 4 жыл бұрын
Bye bye AsyncTask 😀👋 .... Hello Coroutines!!
@buzzminkyunghoon3856
@buzzminkyunghoon3856 4 жыл бұрын
I will still use Async tasks in Flutter
@shaun.august
@shaun.august 4 жыл бұрын
bro, AsyncTask is not going nowhere, all what has changed is name and the way it works and executes in the background. AsyncTask is always there...and will be..
@areebjamaliam
@areebjamaliam 4 жыл бұрын
@@shaun.august LOL. That's r/mAndroid stuff
@edulgl
@edulgl 4 жыл бұрын
@@shaun.august AsyncTask will be forever in our hearts
@harvey7771
@harvey7771 4 жыл бұрын
AsyncTask: I've moved to behind the curtain. :D
@PatricioMoschcovich
@PatricioMoschcovich 4 жыл бұрын
Great video! I am looking forward to learning more about coroutines
@igorg.8624
@igorg.8624 3 жыл бұрын
I am curious why your example observes LiveData or collects Flow directly in the Activity. Isn't it better to use a ViewModel?
@puhgeh
@puhgeh 4 жыл бұрын
Thank you so much! It's time to migrate my livedata to flow!
@no_one1500
@no_one1500 4 жыл бұрын
saddd people are moving to flow etc etc and here i am learning livedata ....
@joaquimley
@joaquimley 4 жыл бұрын
@@no_one1500 Still valuable!
@houssemzaier
@houssemzaier 3 жыл бұрын
you still need if you do databinding.
@dannicron
@dannicron 4 жыл бұрын
So what is the rule for collecting flows in UI controllers? asLiveData().observe(..) when I want to observe hot streams as cold flows and lifecycleScope.launchWhenStarted when I observe e.g. database?
@seshadripera
@seshadripera 3 жыл бұрын
Good alternative for AsyncTask 👍
@isaimosso2764
@isaimosso2764 4 жыл бұрын
Excellent!
@user-ce1om8ku4v
@user-ce1om8ku4v 2 жыл бұрын
Thank you, best man!!
@ViTORossonero
@ViTORossonero 4 жыл бұрын
Snippet at 4:11 has an error: Single.just(readFromDb()) .subscribeOn(Schedulers.io()) is actually equivalent of Single.just(readFromDb()) .observeOn(Schedulers.io()) thus readFromDb() will be ran on the thread createWork is called(main?). And it's not about RxJava, it's about Kotlin(or Java): to call a function(Single.just) we have to calculate its parameters first.
@joaquimley
@joaquimley 4 жыл бұрын
Is it just me or in the first example the: val dbData = readFromDb() should've been "data"? Looks like you are "uploading" to the remote server, the variable is called "data" and it isn't declared in the fun's scope?
@kirill4531
@kirill4531 4 жыл бұрын
yep
@nogueirasjn
@nogueirasjn 4 жыл бұрын
Hello Coroutines!!!
@mahavirprasad9476
@mahavirprasad9476 4 жыл бұрын
Nice bro tq
@LiranBarsisa
@LiranBarsisa 4 жыл бұрын
How do you cancel a job via kotlin coroutines, in the same way you can do for AsyncTask? For when you have AsyncTask instance, you have a choice, of canceling with and without interruption. ON kotlin coroutines, I can see that you have to choose it when you create it. Is it correct?
@StreetsOfBoston
@StreetsOfBoston 4 жыл бұрын
Either call "cancel()" on the CoroutineScope that starts all your coroutines, or call "cancel()" on a "Job"/"Deferred" returned by the call to "launch" or "async" But "cancel()" will not interrupt a blocking thread. It will only cancel a suspending coroutine.
@CaioFaustino
@CaioFaustino 4 жыл бұрын
great video!
@mddi1420
@mddi1420 3 жыл бұрын
It should be interesting to show also how to unit test it
@serensis
@serensis 4 жыл бұрын
How can at the java?..Is it no way?! ㅠㅠ
@pavneetsingh4809
@pavneetsingh4809 4 жыл бұрын
So the live data and the flow (state, shared, channel, etc) will likely to coexist and won't converge?
@gi1dor
@gi1dor 4 жыл бұрын
I don't see any reason to use LiveData if you already use RxJava or Flow, except databindings (which I hope will be fixed at some point too)
@RhexGomez
@RhexGomez 4 жыл бұрын
Databinding might become irrelevant if we fully switch to Compose. Although it is too early to say that.
@gi1dor
@gi1dor 4 жыл бұрын
@@RhexGomez Yes, but it doesn't related to original argument, that LiveData look not relevant if you have proper reactive library. If we talking about databinding, it doesn't mean that with release of Compose in 1-2 years magically all apps who use data bindings are disappeared or rewrite all code to compose
@pavneetsingh4809
@pavneetsingh4809 4 жыл бұрын
@@gi1dor LiveData is lifecycle aware that makes it more efficient when being used in a activity or fragment. Foreseeing a library of lifecycle aware flow and channels, in case of no inbuilt support.
@gi1dor
@gi1dor 4 жыл бұрын
@@pavneetsingh4809 This sounds as repeating of some marketing, than actual argument. "lifecycle aware" just fancy words for subscribing and unsubscribing on particular lifecycle events, Flow not only lifecycle aware, it has structured concurrency which already works with Android lifecycle, and prevents leaks. Example from video about that LiveData unsubscribed on stop and resume on start can be implemented with 5 lines of code with Flow + LifecycleOwner. So no, it's not more efficient, it just has particular default behavior (which by the way not so easy to avoid)
@mahavirprasad9476
@mahavirprasad9476 4 жыл бұрын
Tq bro
@TheDnaitsirc
@TheDnaitsirc 4 жыл бұрын
So... Use or not LiveData? Will there be a stable decision?
@joaquimley
@joaquimley 4 жыл бұрын
For the UI layer? Yes. Anything else? No.
@TheDnaitsirc
@TheDnaitsirc 4 жыл бұрын
Not stable decision certainly. He says that in the video.
@yaroslavkulinich7155
@yaroslavkulinich7155 4 жыл бұрын
@@TheDnaitsirc Flow is a stable decision for repository and data source levels. Livedata is for View and VIewModel levels. StateFlow and SharedFlow are not stable yet but it fully replaces LiveData on mentioned levels.
@antoniokomangyudistira4150
@antoniokomangyudistira4150 4 жыл бұрын
Ugh my head goes 🤯🤯
@BrilTek
@BrilTek 4 жыл бұрын
Why?
@azulamazigh2789
@azulamazigh2789 4 жыл бұрын
me too
@rsolmenra6231
@rsolmenra6231 4 жыл бұрын
good
@mahavirprasad9476
@mahavirprasad9476 4 жыл бұрын
Oh ..
@thurainbowin7164
@thurainbowin7164 3 жыл бұрын
omg... i am just understanding asynTask. It is now deprecated..
@rpintellicus8223
@rpintellicus8223 3 жыл бұрын
You guys have made android tough to learn, can't you make it simpler
@michaelnajera7958
@michaelnajera7958 4 жыл бұрын
I wish Swift had coroutines
@andreberenguel5753
@andreberenguel5753 4 жыл бұрын
Dispatchers are simpler
@_checkit
@_checkit Жыл бұрын
Nothing better than a new API to easily write gotcha code and break your app !
@tubtxheejlischannel72
@tubtxheejlischannel72 4 жыл бұрын
how are you
@okaymak
@okaymak 4 жыл бұрын
Would have been nice if audio and video were in sync.
@hdkloh6857
@hdkloh6857 4 жыл бұрын
Making Simple way more Complex
@petrkubi
@petrkubi 3 жыл бұрын
144p 📹
@kirill4531
@kirill4531 4 жыл бұрын
I used to write Android apps on Java but two years ago I switched to Flutter on Dart. And never regretted since then. Everything is so much easier in Flutter. I remember how AsyncTasks first appeared and became recommended, now its deprecated... I'm tired of it frankly speaking. Why should I learn Kotlin? Why should have created new programming language just for Android? Kotlin is not used anywhere else
@eliasb3857
@eliasb3857 3 жыл бұрын
It seems you took a long distance since you switched to Flutter. Anyways, kotlin isn't only for android, it's also used for multiplatform projects, you can have ios base code and create a backend with kotlin
@kirill4531
@kirill4531 3 жыл бұрын
@@eliasb3857 does the backend language matter? You can create a backend on php, who cares, as long as it sends and receives the data
@eliasb3857
@eliasb3857 3 жыл бұрын
@@kirill4531 rofl, your statement was 'why they'd created a language only for Android'
@Rajmanov
@Rajmanov 4 жыл бұрын
Good bye java!
@IOOISqAR
@IOOISqAR 2 ай бұрын
Get to the point, too much blablabla!
Thinking in Compose
25:27
Android Developers
Рет қаралды 86 М.
Kotlin Coroutines 101 - Android Conference Talks
24:49
Android Developers
Рет қаралды 132 М.
WHO DO I LOVE MOST?
00:22
dednahype
Рет қаралды 80 МЛН
The child was abused by the clown#Short #Officer Rabbit #angel
00:55
兔子警官
Рет қаралды 23 МЛН
small vs big hoop #tiktok
00:12
Анастасия Тарасова
Рет қаралды 30 МЛН
3M❤️ #thankyou #shorts
00:16
ウエスP -Mr Uekusa- Wes-P
Рет қаралды 11 МЛН
Hilt - Android Dependency Injection
28:57
Android Developers
Рет қаралды 51 М.
Fragments: Past, present, and future (Android Dev Summit '19)
18:21
Android Developers
Рет қаралды 75 М.
Kotlin under the hood: Understand the internals (Google I/O'19)
40:46
Android Developers
Рет қаралды 46 М.
Paging with Paging 3
11:07
Android Developers
Рет қаралды 55 М.
State of Kotlin on Android
14:31
Android Developers
Рет қаралды 43 М.
1$ vs 500$ ВИРТУАЛЬНАЯ РЕАЛЬНОСТЬ !
23:20
GoldenBurst
Рет қаралды 1,5 МЛН