How to determine where code runs in Swift Concurrency

  Переглядів 1,621

donny wals

donny wals

День тому

Learn everything you need to know about how you can determine where your code will run when you're using Swift Concurrency. We'll look at tasks, functions, and all the rules that go into determining actor isolation.
This video accompanies this blog post: www.donnywals.com/how-to-dete...
If you want to learn more about Swift Concurrency, I highly recommend that you take a look at my book practicalswiftconcurrency.com
If you're interested in deepening your iOS skills, take a look at my books here: donnywals.com/books.

КОМЕНТАРІ: 19
@chovuse
@chovuse Місяць тому
Your style of tutorial makes it more easier to understand Swift coding. Thanks a Lot !
@DonnyWalsdev
@DonnyWalsdev Місяць тому
Thanks! Happy to hear that
@FloWritesCode
@FloWritesCode 2 місяці тому
This is an interesting topic, thanks a ton for sharing!
@bobdel
@bobdel 22 дні тому
This video is very helpful. I am still confused by the body property. As I understand it, Body is annotated via a protocol to run on MainActor. Is this correct: a view such as the one in your example that does not have a @MainActor annotation, but the function is called from a task modifier inside the Body will run on the MainActor.
@DonnyWalsdev
@DonnyWalsdev 19 днів тому
It's the opposite, unless the function definition itself has a @Mainactor annotation (or in other words, is isolated to the main actor) it doesn't matter where you call it from. The function itself decides where it runs and if it's not isolated to the main actor explicitly it will run in the background even if it was called from a main actor isolated spot
@pmatar
@pmatar 2 місяці тому
Great video, thanks for sharing! And what about the assigning of the result of async func to UI-related piece? How exactly it works and should we care about main thread the same way as with gcd?
@DonnyWalsdev
@DonnyWalsdev 2 місяці тому
Yes you should make sure you assign on the main actor. The easiest way is to make sure that the property you’re assigning too is main actor annotated. That way you’ll never forget because all access to the property will be isolated to the main actor. Alternatively you can use MainActor.run to run a piece of code on the main actor
@pmatar
@pmatar 2 місяці тому
@@DonnyWalsdev oh I see what you mean. Thank you for your response!
@alperenunal3975
@alperenunal3975 2 місяці тому
Btw what’s your xcode theme ? Looks very cool :)
@DonnyWalsdev
@DonnyWalsdev 2 місяці тому
It's "Material theme Ocean high contrast" in VS code
@alperenunal3975
@alperenunal3975 2 місяці тому
If we are using @observable macro on our viewmodel, should we use @mainactor at the same time ?
@DonnyWalsdev
@DonnyWalsdev 2 місяці тому
I would advice that you do on the view, SwiftUI views aren’t main actor isolated by default
@alperenunal3975
@alperenunal3975 2 місяці тому
@@DonnyWalsdev so we should mark View as a main actor ?
@DonnyWalsdev
@DonnyWalsdev 2 місяці тому
@@alperenunal3975IMO, yes that would be a good idea. It's honestly a little surprising to me that Apple doesn't enforce this
@alperenunal3975
@alperenunal3975 2 місяці тому
@@DonnyWalsdev oh, I got it. Thanks for explanation :)
@pmatar
@pmatar 2 місяці тому
private var imageUrl: String? { didSet { dowloadImage() } } func setImage(_ imageUrl: String?, placeholder: UIImage?) { self.image = placeholder self.imageUrl = imageUrl } private func dowloadImage() { guard let imageUrl else { return } Task { @MainActor in do { let loadedImage = try await ImageLoader.shared.downloadImage(from: imageUrl) if imageUrl == self.imageUrl { image = loadedImage } } catch { debugPrint(error) } } } That’s the example of what I mean
@DonnyWalsdev
@DonnyWalsdev 2 місяці тому
This would work perfectly fine. Your task is run on the main thread which means the assignment you do at the end is on the main actor
@pmatar
@pmatar 2 місяці тому
@@DonnyWalsdev but won’t the download will also happen on the main thread, instead of global/background?
@DonnyWalsdev
@DonnyWalsdev 2 місяці тому
@@pmatar no, because the download function itself most likely is defined non isolated. If not, you can be certain that URLSession’s networking is non isolated. So that code run on the global executor. While you await, the main thread is free to work on other stuff because an await doesn’t block; it frees the actor up for other work
Your Brain 🧠 on Swift Concurrency - iOS Conf SG 2023
30:38
iOS Conf SG
Переглядів 6 тис.
Programming in Modern C with a Sneak Peek into C23 - Dawid Zalewski -  ACCU 2023
1:27:41
Google Cloud VMware Engine: DR & Backup (GCVE)
3:22
Jason Meers - VMware & Google Cloud
Переглядів 9
Actor Reentrancy in Swift explained
20:18
donny wals
Переглядів 554
An introduction to JSON Parsing in Swift
16:44
donny wals
Переглядів 631
Big Tech AI Is A Lie
16:56
Tina Huang
Переглядів 56 тис.
Svelte 5: Compiler, Signals, and Web App Performance - JSJ 627
1:21:52
Top End Devs
Переглядів 2,4 тис.
How to be a git expert
46:26
Floating Little Leaves of Code
Переглядів 139 тис.
How to get started with Swift Concurrency 🧵 (Beginner Tutorial)
2:13:35
Vincent Pradeilles
Переглядів 3,9 тис.
You might not need useEffect() ...
21:45
Academind
Переглядів 124 тис.
BMW просто издевается над нами! Силы на исходе…
1:34:41
ИЛЬДАР АВТО-ПОДБОР
Переглядів 3 млн
14,000 Domino pieces Fail!
0:12
VIP MOTORS
Переглядів 62 млн
Находка для розыгрышей с вайлдберриз озон #wb #shorts
0:15
BMW просто издевается над нами! Силы на исходе…
1:34:41
ИЛЬДАР АВТО-ПОДБОР
Переглядів 3 млн