I've had a hard time writing things that help people get started with concurrency. I want to fix that!
Here's a step-by-step discussion of how to make a network request with SwiftUI. It starts with GCD and moves to async/await. Includes some common techniques you might want to avoid too.
You do have to understand a little, but I tried my best to make it approachable.
https://www.massicotte.org/step-by-step-network-request
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem that might be exactly what I was looking for, thank you 🙏🏻
=> More informations about this toot | More toots from myell0w@mindly.social
@mattiem ha, the second sentence hit me right in the gut 😅
=> More informations about this toot | More toots from bens@mastodon.xyz
@bens My people 👊🏻
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem @bens This is more or less how my recent adventure with Rust looked like 🦀
=> More informations about this toot | More toots from mackuba@martianbase.net
@mattiem when I first was coding on a Mac (early 90s) every couple of compiles was a reboot. The Mathematica computations I ran over night to simulate elliptic integrals worked without crashing every third time.
=> More informations about this toot | More toots from dimsumthinking@mastodon.social
@mattiem I appreciate you for your efforts to clarify Swift Concurrency!
One of the things I always find challenging when introduced to new terminology is how the term actually relates to the concept.
In your section on isolation, you introduce the term but the explanation doesn’t use the term. This is common in technical writing. But the reader is left to infer how the term relates to the concepts introduced. Seeing how the terms are used helps in building intuition around the concepts.
=> More informations about this toot | More toots from humblehacker@mastodon.online
@humblehacker Hmmm this is good feedback. I’m going to re-read more closely.
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem This is, by far, the most understandable explanation of concurrency. Followed you top to bottom, and it’s totally to the credit of your writing!
It certainly helped that you picked a very common scenario and went through the “evolution” of the code from GCD to the new concurrency, but the explanation along the way (nonisolated did sound unsafe!) make the whole a cohesive exceptional starting point. (1/2)
=> More informations about this toot | More toots from cdf1982@iosdev.space
I really hope that over time you’ll write more “basic” documentation like this one, because it’s truly excellent. (2/2)
=> More informations about this toot | More toots from cdf1982@iosdev.space
@cdf1982 I'm sooo happy to hear this!
I have three half-written posts and none of them are beginner stuff. But, I've kinda put those on hold for now. I think the introductory stuff is just more important.
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem Well, I cannot say I am objective on this… :)
In a way, this first post is the most important, because even a superficial understanding of isolation can make a long way in demystifying the new concurrency model. But please keep ‘em coming when you have the time :)
=> More informations about this toot | More toots from cdf1982@iosdev.space
@mattiem this is amazing! Thank you! 🙏
=> More informations about this toot | More toots from agis@mastodon.social
@mattiem This is appreciated sooo much!! Today I’ve enabled Swift 6 language mode in my app and I already know this is exactly what I need to get started with the migration.
=> More informations about this toot | More toots from indigo@techhub.social
@mattiem thank you. This is really helpful and I think I’ll come back to it several times.
I get most of the basic stuff about Swift concurrency by now I think, except “nonisolated” and “redundant await MainActor.run”, this post does a great job explaining both
=> More informations about this toot | More toots from jaanus@iosdev.space
@mattiem I appreciate your relentless drive to inform and teach. Thank you. 🙏🏻 👏🏻
=> More informations about this toot | More toots from titociuro@mstdn.social
@titociuro I’m having fun doing it!
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem Nice article, thanks for that! It especially clicked for me when reading alternative 2, where using await will switch back to the MainActor, when the called function itself is declared as such.
I've always thought of await as a point where it will switch threads, but now understanding when and how to use nonisolated I feel more way more confident about this.
=> More informations about this toot | More toots from saschagordner@mastodon.social
@saschagordner That’s wonderful to hear! This stuff takes practice.
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem Amazing post, thank you! I'll make sure to share. One thing I've been noodling on... in my mind, "nonisolated" is translated to "AnyActor" or "NotMainActor" or something. I kinda wish we could just use "@MainActor" and "@BackgroundActor" or something, ya know?! It's just that "nonisolated" is such a weird and scary word!
=> More informations about this toot | More toots from trevorturk@ruby.social
@trevorturk Thank you!!
It’s actually totally possible to make a BackgroundActor. But you know what? Isolation has a type and it is “(any Actor)?”. Nonisolated literally means nil here!
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem Great write up Matt!
Quick one: am I making wrong assumptions if I think of nonisolated
as nonmainactor
?
=> More informations about this toot | More toots from diegopetrucci@mastodon.social
@diegopetrucci Thank you!
You are close, but not quite. To be isolated, you need an actor. The type of isolation is “(any Actor)?”. Nonisolated literally is nil. No actor at all!
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem Matt thanks a bunch for this. Top step of the concurrency podium. 🥇
=> More informations about this toot | More toots from martind@mastodon.online
@mattiem this legitimately solved a problem I had today that was exactly this example. Thank you for this explanation!
=> More informations about this toot | More toots from cameronmcefee@mastodon.social
@cameronmcefee 🙌🏻
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem This was excellent, thank you! I hadn't realized you could use nonisolated for the “get this off the main actor" part.
=> More informations about this toot | More toots from juri@mastodon.social
@mattiem matt thank your for publishing this, I think many a developer will appreciate this highly because it goes beyond just the code and in such great detail about the mechanics and the reasoning behind the compiler diagnostics
=> More informations about this toot | More toots from icanzilb@mastodon.social
@icanzilb @mattiem As a developer I can confirm I appreciate this highly!
I learned something new, but still don't quite understand it: I didn't know that nonisolated
would not just be "use whatever thread you're on", but actually encourages using something else than the MainActor!
My gut would have told me I need to put the work onto another actor
instead, for example, to make this operation non-blocking.
=> More informations about this toot | More toots from ctietze@mastodon.social
@ctietze @icanzilb @mattiem I’ve been mulling if there’s a better word we could have used than nonisolated because while it is highly accurate IMO, it does create a lot of confusion.
I think a more intuitive word would have been nonactor.
=> More informations about this toot | More toots from cocoaphony@mastodon.social
@cocoaphony @ctietze @icanzilb Yeah causing lots of confusion definitely. “nonactor” is equally accurate I guess…
There might be discussion about this in the proposal that introduced it. I haven’t read that one.
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem so it's really elsewhere
, or is it somewhere
(including maybe where you are, e.g. MainActor), effectively?
=> More informations about this toot | More toots from ctietze@mastodon.social
@ctietze @mattiem it’s “elsewhere.” It’s promised not to be on any actor.
=> More informations about this toot | More toots from cocoaphony@mastodon.social
@ctietze Getting really accurate here is complex. There was a proposal introduced that allows you to exert more control, even in this case I’m pretty sure, for performance optimization purposes.
But you should always think of “nonisolated” as elsewhere and never where you are. Unless where you are is also nonisolated 😅.
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem @cocoaphony Thanks you two, I can remember that!
=> More informations about this toot | More toots from ctietze@mastodon.social
@cocoaphony @ctietze @icanzilb @mattiem Isn’t something that is nonisolated “integrated”? Probably would be more confusing in this case, but from a language point of view, might be more correct.
=> More informations about this toot | More toots from drewmccormack@mastodon.cloud
@drewmccormack @cocoaphony @ctietze @icanzilb Hmm I’m not sure! I have not seen that word used in this context before.
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem @cocoaphony @ctietze @icanzilb Would not be good in this context, no. But technically, from a purely language point of view, “nonisolated” = “integrated”. Maybe that is the problem. The word doesn’t convey what they want it to. It’s more like it is “unaffiliated”.
=> More informations about this toot | More toots from drewmccormack@mastodon.cloud
@drewmccormack @cocoaphony @ctietze @icanzilb Isolation has a type. It is “(any Actor)?”. Nonisolated is “nil” here. No actor, and isolation comes from actors. It’s definitely confusing though because this comes up all the time.
=> More informations about this toot | More toots from mattiem@mastodon.social
@ctietze @icanzilb @mattiem my confusion starts at the negation already: I‘ve learned the hard way to avoid e.g. negating booleans as they will create brainpunk later on…
=> More informations about this toot | More toots from omich@mastodon.social
@omich @ctietze @icanzilb Yeah I agree. This is a tricky problem because its purpose is negation. “Do this everywhere except in these two spots”
I think it is actually is possible to structure your code such that you never need to use “nonisolated” without giving up its functionality. But that requires a little more understanding of what’s going on.
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem Great article. Super helpfully illustrated based on a comprehensible example 🙏
=> More informations about this toot | More toots from awoerner@mastodon.social
@mattiem Bless you, @mattiem
=> More informations about this toot | More toots from jpurnell@mastodon.social
@mattiem I’ve been following along your concurrency spelunking and explaining, and I think you’ve done a great job here breaking down a lot of concepts that trip people up. And I can see from the reactions how much this has resonated with folks! Awesome work 😄
=> More informations about this toot | More toots from dachary@dacharycarey.social
@dachary Thank you!
And yes wow did this one take off. I didn’t think it was that great haha!
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem What an absolutely wonderful snapshot for this moment in time. Now this is the reference baseline for everyone use, and is just going to get better. Very well done.
=> More informations about this toot | More toots from noworkie@mastodon.social
@noworkie Thank you so much!!
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem Turns out even though my code was compiling I had a bunch of stuff running on the MainActor when I thought it wasn't. I'm using nonisolated a lot more intentionally now. Thanks!
=> More informations about this toot | More toots from ryanlintott@mastodon.social
@ryanlintott 💪🏻
=> More informations about this toot | More toots from mattiem@mastodon.social
@mattiem thanks for clarifying on nonisolated, it helped me simplify lots of code that overused custom actors when all I needed was background work.
That said, I wish i could use nonisolated in closures, so I don't have to explicitly name a function in order to run nonisolated, as in:
sections = await { nonisolated async EventGroupsSection.sections(withStats: dayStats) }()
async let _sections = {
EventGroupsSection.sections(withStats: stats)
}()
sections = await _sections
at this point i lean towards just declaring an extra local function because of that extra boilerplate:
nonisolated func fetchSections() async -> [EventGroupsSection] {
EventGroupsSection.sections(withStats: stats)
}
sections = await fetchSections()
text/gemini
This content has been proxied by September (3851b).