Network Timeouts with Kotlin Coroutines

preview_player
Показать описание

In this video I show you how to handle network timeouts using Kotlin coroutines.

In the past, network timeouts have been notoriously difficult to handle on android. AsyncTask, Executors, Threads, all these components made it very difficult to handle errors when making network requests.

RxJava and LiveData made is slightly simpler, and Kotlin coroutines takes it a step further. With Kotlin coroutines there's many different ways to handle timeouts and cancellations. This is a simple example showing you one of those ways using the "withTimeoutOrNull" coroutine launcher method.

Рекомендации по теме
Комментарии
Автор

"if this is helpful", seriously?
This is surely very helpful. Thanks for the video.

dhruvtatran
Автор

One of my first android tutorials in my life was one of your videos. The one where you show how to implement an OnClick method for a Button and here I am again, after almost 4 years and 2 jobs as an Android developer, learning how to use Coroutines. Thanks Mitch

LuisAguilar-hplq
Автор

10:28
That's why you should never use tight timeouts.

A time out should be of a length that can not be reached when executed correctly.

Using a tight timeout makes stuff time out that was executed correctly but under suboptimal circumstances.
That's usually not your goal.

OggerFN
Автор

coroutines is just perfect. it will replace a lot of inconvenient stuff.

thx for the series.

gabormiklay
Автор

Thanks so much for the great content. I'm moving my Java project to Kotlin after seeing how much better these Kotlin coroutines are than AsyncTasks 🤢

darrenfinch
Автор

Best video by you for all the new stuff in Android

Keep Going Good bro, thanks

Since
Автор

Cool.. 👍
Make more videos on Coroutines.

shailendraharinkhede
Автор

When will Open API Android App lectures be available and how many would be there and approx when will the series end of Open Api?

buzzminkyunghoon
Автор

Are coroutines analogous to contextualized JavaScript Promises?

blueheartorangeheart
Автор

Awesome, I hope I can get more videos about coroutine

harveytai
Автор

Funny thing, Retrofit supports timeout exceptions so you can actually set a writeTime and it should throw the exception where you need it. But for some unknown reasons, the timeouts never worked for me where they should. What makes it even more sinister is the fact that the timeout exception gets thrown every time I use debugger or profiler, but never actually when I am just running the app. It keeps loading forever. So I did a trick, just like you, with coroutines and delay. I created an extension function where you pass the right context for the scopes (lifecycleScope for activity and viewModelScope for viewModel) and handled my scenarios there. And then I execute the task, if it completes faster than the timeout/delay, I destroy the job with the delay and start the next activity, otherwise I wait for the delay and display a retry button after 14 seconds in case of failure. Kotlin rocks!

radumarinescu
Автор

Awesome and thanks for the great content .

samha
Автор

These videos are remarkably good. You're really good.

AntonioGonzalezsanchez
Автор

Another very useful tutorial. Very well put together and explained. Keep up the good work.

sudhir
Автор

Very usefull, excellent video regards from Caracas, Venezuela

VenewebTV
Автор

Firstly thanks for such a great video .
Now, 1 question . While I was trying the above example, I created 2 jobs with via launch() and other via withTimeoutOrNull(), within same single scope inside method fakeApiRequest() AND increased the delay time inside getResult*FromApi() to 3 second each and JOB_TIMEOUT to 4000L . Now when I execute, I see the 2 different jobs running in parallel . And in video u said the 2nd job will execute after the completion of first job . Can you please help me in understanding my concern.
One thing more if you change the order of job, say if you call launch() before withTimeoutOrNull(), we see parallel execution BUT if we call withTimeoutOrNull() before launch(), we see sequential execution !!

manishsahu
Автор

@CodingWithMitch Please make more videos.

muazzamabbas
Автор

Wow..Kotlin owes u buddy...thanks a lot fr this series...

bjugdbjk
Автор

why suspend funs?
Are they used only to restrict a fun not to be used outside coroutine scope or other suspend functions ?

androidpc
Автор

Very helpful Sir. Please also add a full fledged course based on MVVM, Coroutines, Dagger and Unit Testing.

mohdaquib