Download images with Async/Await, @escaping, and Combine | Swift Concurrency #2

  Рет қаралды 29,242

Swiftful Thinking

Swiftful Thinking

Күн бұрын

Alright let's start using async and await! In this video we take a look at the 3 main ways to write asynchronous code in Swift. We will download an image using:
(1) @escaping closures
(2) Combine
(3) Async / Await
By writing out these three techniques side-by-side, it becomes much easier to understand the progression of the Swift language and just how ~awesome~ Swift Concurrency is. We are JUST getting started!
API: picsum.photos/200
Next video: • How to use async / awa...
Previous video: • How to use Do, Try, Ca...
🤙 WELCOME BACK 🤙
WEBSITE: www.swiftful-t...
DISCORD: / discord
GITHUB: github.com/Swi...
SAY THANKS: www.buymeacoff...

Пікірлер: 70
@lincolndickerson1293
@lincolndickerson1293 2 жыл бұрын
Async await is amazing but the discussion of the three processes if pure coding gold! This should be required watching for every swift developer.
@keithweiss229
@keithweiss229 2 жыл бұрын
When you have a thowing function, you don't have to wrap with do, try, catch unless you want to translate the error to a new error or remove the throws decorator. For example, download() method can be simplified to: func download() async throws -> UIImage? { let (data, response) = try await URLSession.shared.data(for: URLRequest(url: url)) return try handleResponse(data, response) } Any error thrown by URLSession will be thrown to the caller. In this example, I rewrote handleResponse() to also throw errors. I enjoy your content and only point out this tip in hopes to supplement your superb instruction.
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
Hey Keith, these comments are always welcome on my channel! We’re all here to learn and this is a really great point. There’s probably a bunch of functions in the upcoming videos where this same logic applies. Thanks for the comment - I’m sure others will see this too 👍🙏
@keithweiss229
@keithweiss229 2 жыл бұрын
@@SwiftfulThinking I only point it out, because I have started doing much more server-side development and this tip saves a lot of time. One other small tip is that you can test if the response status code is within a range with bit of syntactic sugar: 200…299 ~= response.statusCode. Thank you for your channel, you have brought my skills to the next level.
@andresraigoza2082
@andresraigoza2082 2 жыл бұрын
@@keithweiss229 Very useful comments, Thank you so much Keith! This made my code much cleaner
@irshad365
@irshad365 Күн бұрын
Your videos are really good, to the point and clean. THANKS
@sfunke2004
@sfunke2004 2 жыл бұрын
I would upvote this series multiple times if I could. Thank you so much from a fellow Android developer, who has to do iOS from time to time! Nice to see development patterns converge between the platforms (SwiftUI/Compose, async/await vs Kotlin coroutines, ViewModels, ...).
@FelipeVeiga
@FelipeVeiga 2 жыл бұрын
Amazing video and explaining techniques. Thanks for sharing
@eric9964
@eric9964 2 жыл бұрын
Great video! Glad to see you're back at it!
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
Thanks Eric! 🤙
@nalindawickramarathna8730
@nalindawickramarathna8730 9 ай бұрын
Golden content that you cannot find anywhere else for sure. Thanks mate!
@hao-bz
@hao-bz 4 ай бұрын
the best swift & SwiftUI related channel !
@kavinda_dilshan_paramsoodi
@kavinda_dilshan_paramsoodi 2 жыл бұрын
Well.... I will build all my the upcoming projects with async await from now on. Thanks nick. 👨🏻‍💻
@andresraigoza2082
@andresraigoza2082 2 жыл бұрын
Great explanation!!! Thank you so much. I just wanted to suggest that since I work with a small screen I would appreciate a ton if you could please make the font of your code a little bit bigger for future videos. Thanks again and please Keep up the great work 👍🏻
@froggy58400
@froggy58400 Жыл бұрын
Thanks so much Nick for this video and the whole series! I just found a new way (iOS 15+) too, it seems, very easily download an image asynchronously. Here is an example: AsyncImage(url: URL(string: track.imageUrl), scale: 1.0) { image in image .resizable() .scaledToFit() } placeholder: { ProgressView() } .frame(width: 50, height: 50) .cornerRadius(5)
@hamidehsheikh2619
@hamidehsheikh2619 2 жыл бұрын
Thank you Nick, you're the best😍
@BenjaminEggerstedt
@BenjaminEggerstedt 2 жыл бұрын
I couldn’t really decide if I should use @escaping, Combine or async/await and now I feel that I want to use async/await in my app. Is it Monday yet? Looking forward to the next videos!
@kenjyfukuda
@kenjyfukuda 2 жыл бұрын
Crystal-clear. Thank you!
@bobbynwm
@bobbynwm Жыл бұрын
Thanks a lot mate! Really good content and helpful to see different perspectives and ways to improve the code
@richardhasson265
@richardhasson265 2 жыл бұрын
Great stuff as always. Thanks.
@rachelradford5193
@rachelradford5193 2 жыл бұрын
I'm so happy you're making this series. I've been struggling to understand how to make an API call in Swift, natively. I'm curious why you didn't use AsyncImage? Or, is that going to be covered later?
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
I covered AsyncImage in the beginner playlist here: kzbin.info/www/bejne/h5yYpGBsocmBnKM. In this playlist, we're focused on actually writing async/await code, which you don't need to do when using AsyncImage.
@KulturanCov
@KulturanCov 2 жыл бұрын
@@SwiftfulThinking Can AsyncImage be combined with our async/await code? What about its placeholder code?
@98motya
@98motya 2 жыл бұрын
Nick! I probably love you!
@98motya
@98motya 2 жыл бұрын
I really was waiting for this video especially from you!
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
One Love!! 🌴☀️
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
It get's better don't worry 😎
@markaurelius61
@markaurelius61 2 жыл бұрын
In the downloadWithAsync function, could you not have left out the do .. catch throw structure? Since the function is marked as "throws", won't any errors just propagate up automatically?
@terry1992
@terry1992 2 жыл бұрын
You have helped us so much. THANK YOU.
@danrleiribeiro7959
@danrleiribeiro7959 Жыл бұрын
Cara, você tem uma didática incrível, parabéns !! :)
@faikcoskuncakr7418
@faikcoskuncakr7418 2 жыл бұрын
Again great video. Thanks
@1slyboy
@1slyboy 11 ай бұрын
Two questions (maybe you answer in a subsequent video): 1. What if MainActor.run throws? It appears it can but it wasn't in a do-catch nor was the function marked as throws. 2. You indicated that [weak self] wasn't needed but it wasn't clear why. Thanks!
@SwiftfulThinking
@SwiftfulThinking 11 ай бұрын
If your call throws, then you can put it in a do-catch block. Towards the end of the playlist, there’s an entire video explaining [weak self] 😅
@foggydawning
@foggydawning Жыл бұрын
Wow! Amazing 😍
@josemanuelortizsanchez4983
@josemanuelortizsanchez4983 2 жыл бұрын
Thanks for this video!
@ponnywang149
@ponnywang149 2 жыл бұрын
非常棒的课程,非常感谢!!
@thahiraf1317
@thahiraf1317 2 жыл бұрын
Please make tutorial with rest api. You explanation so good.
@AndresGutierrez-nb6qr
@AndresGutierrez-nb6qr 11 ай бұрын
@SwiftfulThinking, Great video, love your content! I was wondering if it would be best to create the Task in the ViewModel, rather than the View? Lastly, if we added the @MainActor macro to the ViewModel, could we effectively remove the await MainActor code from our function? Thanks a bunch!
@SwiftfulThinking
@SwiftfulThinking 11 ай бұрын
I added a few videos toward the end of this playlist to dive into these questions: How to use MVVM with Async Await | Swift Concurrency #14 kzbin.info/www/bejne/haGtlHykiL-jmZo
@AndresGutierrez-nb6qr
@AndresGutierrez-nb6qr 11 ай бұрын
@@SwiftfulThinking appreciate you brother!
@ShooMPozitiV
@ShooMPozitiV 2 жыл бұрын
The best of the best as always )
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
Thank you Сергей!
@bhuwin6186
@bhuwin6186 Жыл бұрын
Best guides, needs more exposure.
@SwiftfulThinking
@SwiftfulThinking Жыл бұрын
Thanks 👍👍
@cinquain0
@cinquain0 2 жыл бұрын
Interesting... I'm not sure if async/await on Swift is as elegant as it is on Javascript. You don't need all this stuff like Task or Actors... I don't know maybe the syntax is still a bit foreign to me hence my initial revulsion.
@mehekpalrecha4262
@mehekpalrecha4262 Жыл бұрын
is concurrency features only available in newer versions? I have big sur version 11 and xcode 12 IOS 14 and it says @mainactor is unknown. Do I have to upgrade to inplement concurrency?
@Faiiivourful
@Faiiivourful 2 жыл бұрын
Would you demonstrate how to fetch images from firebase storage as well, thank you
@alxkub
@alxkub Жыл бұрын
That combine-based example looks like a bunch of really really strange incantations for those unfamiliar with Combine like me.🤪
@vladislavbogucharov6256
@vladislavbogucharov6256 2 жыл бұрын
Best content!
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
Thanks Vladislav!!
@farhad28881987
@farhad28881987 2 жыл бұрын
Hey Nick, any chance you can add this project to your GitHub repo?
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
Omg I keep forgetting… give me an hour 😅
@farhad28881987
@farhad28881987 2 жыл бұрын
@@SwiftfulThinking you are the best
@vlastos
@vlastos 2 жыл бұрын
Watch out for imlicit capture in `Task { }`. You've created a leak there, and the operation wont cancel, as it does correctly with combine version
@mhmtkrnlk
@mhmtkrnlk 2 жыл бұрын
Async await is ok but im sick of using it on other languages. My taste is combine. Thnx nick for video!
@dugrut1325
@dugrut1325 2 жыл бұрын
...over my head, I think I drowned.
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
😂 you’re cracking me up. I’ve been reading your comments but haven’t had a minute to go back and respond yet. The fact that you’re up to this playlist is promising tho 👏👏👏
@OhreoOhs
@OhreoOhs 2 жыл бұрын
👍👍👍
@frydaydeep6674
@frydaydeep6674 2 жыл бұрын
So, Cap Nick, it means I can stop digging Combine ?
@joeybodnar
@joeybodnar 2 жыл бұрын
15:40, we don't need to write the arguments for handleResponse because they are implied--very disappointed to see swift head in this direction. it just makes code so hard to read and figure out what is happening at a glance.
5 ай бұрын
Please activate the Donate button here on KZbin too! You'll earn much more! You deserve every penny!
@aliquewilliams3080
@aliquewilliams3080 Жыл бұрын
Combine is not the "old" way.
@shadyforge
@shadyforge Жыл бұрын
Hey, I think you need to spend more time explaining/teaching *why/what* you are coding, instead of reciting the code as you type it. You do it in a few areas, but most of the video you are not. Important to separate a tutorial from a lecture.
@SwiftfulThinking
@SwiftfulThinking Жыл бұрын
Welcome to the channel! The playlists are all ordered, so I have to assume some knowledge before teaching a new subject. If you’re confused on how to use @escaping or Combine, I would recommend watching the Continued Learning playlist before this one.
@nickfloussov185
@nickfloussov185 2 ай бұрын
The (only) problem I have with async/await approach is a LONG chain of async and await keywords throughout the code, whereas for combine (and handler) all is ENCAPSULATED within loader. P.S. it would be good if you add couple (more) words about why it is preferred to use MainActor vs. DispatchQueue.main.async.
@hayulu-do1mb
@hayulu-do1mb Жыл бұрын
URLSession.shared.dataTask(with: url) { data, response, error in
@danielcrompton7818
@danielcrompton7818 3 ай бұрын
Thanks so much but there’s one thing that doesn’t make sense: why are we not supposed to use DispatchQueue.main.async inside the Task {}
How to use async / await keywords in Swift  | Swift Concurrency #3
20:40
Swiftful Thinking
Рет қаралды 26 М.
Download JSON from API in Swift with Combine | Continued Learning #23
34:43
ДЕНЬ УЧИТЕЛЯ В ШКОЛЕ
01:00
SIDELNIKOVVV
Рет қаралды 4,2 МЛН
She's very CREATIVE💡💦 #camping #survival #bushcraft #outdoors #lifehack
00:26
REAL 3D brush can draw grass Life Hack #shorts #lifehacks
00:42
MrMaximus
Рет қаралды 11 МЛН
VAMPIRE DESTROYED GIRL???? 😱
00:56
INO
Рет қаралды 7 МЛН
How to use Do, Try, Catch, and Throws in Swift | Swift Concurrency #1
26:34
Swift API Calls for Beginners (Networking) - Async Await & JSON
25:35
How to use Global Actors in Swift (@globalActor) | Swift Concurrency #10
23:50
Run ALL Your AI Locally in Minutes (LLMs, RAG, and more)
20:19
Cole Medin
Рет қаралды 168 М.
Threads and Grand Central Dispatch in Swift
22:14
Stewart Lynch
Рет қаралды 21 М.
ДЕНЬ УЧИТЕЛЯ В ШКОЛЕ
01:00
SIDELNIKOVVV
Рет қаралды 4,2 МЛН