How to determine where code runs in Swift Concurrency

  Рет қаралды 2,325

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.

Пікірлер: 21
@user-jj2jf7hf5q
@user-jj2jf7hf5q Ай бұрын
Kudos to the author. First time I hear simple and elaborated explanation about threads which will be used to run task or specific method.
@DonnyWalsdev
@DonnyWalsdev Ай бұрын
Thank you!
@chovuse
@chovuse 4 ай бұрын
Your style of tutorial makes it more easier to understand Swift coding. Thanks a Lot !
@DonnyWalsdev
@DonnyWalsdev 4 ай бұрын
Thanks! Happy to hear that
@FloWritesCode
@FloWritesCode 5 ай бұрын
This is an interesting topic, thanks a ton for sharing!
@pmatar
@pmatar 5 ай бұрын
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 5 ай бұрын
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 5 ай бұрын
@@DonnyWalsdev oh I see what you mean. Thank you for your response!
@bobdel
@bobdel 3 ай бұрын
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 3 ай бұрын
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
@alperenunal3975
@alperenunal3975 5 ай бұрын
Btw what’s your xcode theme ? Looks very cool :)
@DonnyWalsdev
@DonnyWalsdev 4 ай бұрын
It's "Material theme Ocean high contrast" in VS code
@alperenunal3975
@alperenunal3975 5 ай бұрын
If we are using @observable macro on our viewmodel, should we use @mainactor at the same time ?
@DonnyWalsdev
@DonnyWalsdev 5 ай бұрын
I would advice that you do on the view, SwiftUI views aren’t main actor isolated by default
@alperenunal3975
@alperenunal3975 5 ай бұрын
@@DonnyWalsdev so we should mark View as a main actor ?
@DonnyWalsdev
@DonnyWalsdev 4 ай бұрын
@@alperenunal3975IMO, yes that would be a good idea. It's honestly a little surprising to me that Apple doesn't enforce this
@alperenunal3975
@alperenunal3975 4 ай бұрын
@@DonnyWalsdev oh, I got it. Thanks for explanation :)
@pmatar
@pmatar 5 ай бұрын
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 5 ай бұрын
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 5 ай бұрын
@@DonnyWalsdev but won’t the download will also happen on the main thread, instead of global/background?
@DonnyWalsdev
@DonnyWalsdev 5 ай бұрын
@@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
A clash of kindness and indifference #shorts
00:17
Fabiosa Best Lifehacks
Рет қаралды 132 МЛН
A little girl was shy at her first ballet lesson #shorts
00:35
Fabiosa Animated
Рет қаралды 13 МЛН
Your Brain 🧠 on Swift Concurrency - iOS Conf SG 2023
30:38
iOS Conf SG
Рет қаралды 8 М.
Actor Reentrancy in Swift explained
20:18
donny wals
Рет қаралды 1,4 М.
Swift Concurrency Manifesto | Chris Lattner and Lex Fridman
10:59
Using Closures as Dependencies
15:18
donny wals
Рет қаралды 1,1 М.
Customizing how Codable objects map to JSON data
9:32
donny wals
Рет қаралды 487
JavaScript Pro Tips - Code This, NOT That
12:37
Fireship
Рет қаралды 2,5 МЛН
I've been using Redis wrong this whole time...
20:53
Dreams of Code
Рет қаралды 344 М.
Nature's Incredible ROTATING MOTOR (It’s Electric!) - Smarter Every Day 300
29:37
Что делать если забыл ОЧКИ??? #моястихия #swimming #юмор #fun
0:23
МОЯ СТИХИЯ | ПЛАВАНИЕ | МОСКВА
Рет қаралды 1,7 МЛН
How to drink orange juice legally @kahoko6607
0:29
OHIOBOSS SATOYU
Рет қаралды 22 МЛН
Самый ДОБРЫЙ мальчик!😎
1:00
Petr Savkin
Рет қаралды 6 МЛН
4 Признака того, что Жена Злится
0:30
Petya English
Рет қаралды 1,8 МЛН