Ancestors

Toot

Written by Matt Massicotte on 2024-08-14 at 12:46

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

Descendants

Written by Matthias on 2024-08-14 at 12:47

@mattiem that might be exactly what I was looking for, thank you 🙏🏻

=> More informations about this toot | More toots from myell0w@mindly.social

Written by Ben Scheirman on 2024-08-14 at 12:57

@mattiem ha, the second sentence hit me right in the gut 😅

=> More informations about this toot | More toots from bens@mastodon.xyz

Written by Matt Massicotte on 2024-08-14 at 13:08

@bens My people 👊🏻

=> More informations about this toot | More toots from mattiem@mastodon.social

Written by Kuba Suder • @mackuba.eu on 🦋 on 2024-08-14 at 16:45

@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

Written by dimsumthinking on 2024-08-14 at 13:23

@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

Written by David Whetstone on 2024-08-14 at 13:38

@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

Written by Matt Massicotte on 2024-08-14 at 13:40

@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

Written by Cesare Forelli on 2024-08-14 at 13:52

@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

Written by Cesare Forelli on 2024-08-14 at 13:52

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

Written by Matt Massicotte on 2024-08-14 at 14:00

@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

Written by Cesare Forelli on 2024-08-14 at 14:04

@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

Written by Agis Tsarampoulidis on 2024-08-14 at 14:24

@mattiem this is amazing! Thank you! 🙏

=> More informations about this toot | More toots from agis@mastodon.social

Written by Krystian Kopeć on 2024-08-14 at 14:42

@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

Written by Jaanus Kase on 2024-08-14 at 14:58

@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

Written by Tito Ciuro on 2024-08-14 at 16:00

@mattiem I appreciate your relentless drive to inform and teach. Thank you. 🙏🏻 👏🏻

=> More informations about this toot | More toots from titociuro@mstdn.social

Written by Matt Massicotte on 2024-08-14 at 16:17

@titociuro I’m having fun doing it!

=> More informations about this toot | More toots from mattiem@mastodon.social

Written by Sascha Gordner on 2024-08-14 at 16:46

@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

Written by Matt Massicotte on 2024-08-14 at 16:49

@saschagordner That’s wonderful to hear! This stuff takes practice.

=> More informations about this toot | More toots from mattiem@mastodon.social

Written by Trevor Turk on 2024-08-14 at 18:20

@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

Written by Matt Massicotte on 2024-08-14 at 18:38

@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

Written by Diego Petrucci on 2024-08-14 at 22:20

@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

Written by Matt Massicotte on 2024-08-14 at 23:04

@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

Written by Martin Dufort on 2024-08-14 at 22:51

@mattiem Matt thanks a bunch for this. Top step of the concurrency podium. 🥇

=> More informations about this toot | More toots from martind@mastodon.online

Written by Cameron McEfee on 2024-08-15 at 03:16

@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

Written by Matt Massicotte on 2024-08-15 at 10:11

@cameronmcefee 🙌🏻

=> More informations about this toot | More toots from mattiem@mastodon.social

Written by Juri Pakaste on 2024-08-15 at 05:17

@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

Written by Marin Todorov on 2024-08-15 at 06:52

@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

Written by Christian Tietze on 2024-08-15 at 08:36

@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

Written by Rob Napier on 2024-08-15 at 10:31

@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

Written by Matt Massicotte on 2024-08-15 at 10:34

@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

Written by Christian Tietze on 2024-08-15 at 10:55

@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

Written by Rob Napier on 2024-08-15 at 10:57

@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

Written by Matt Massicotte on 2024-08-15 at 10:59

@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

Written by Christian Tietze on 2024-08-15 at 11:36

@mattiem @cocoaphony Thanks you two, I can remember that!

=> More informations about this toot | More toots from ctietze@mastodon.social

Written by Drew McCormack on 2024-08-15 at 20:23

@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

Written by Matt Massicotte on 2024-08-15 at 21:08

@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

Written by Drew McCormack on 2024-08-15 at 21:17

@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

Written by Matt Massicotte on 2024-08-15 at 21:22

@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

Written by Oliver on 2024-08-15 at 11:02

@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

Written by Matt Massicotte on 2024-08-15 at 11:13

@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

Written by Andy Woerner on 2024-08-15 at 08:55

@mattiem Great article. Super helpfully illustrated based on a comprehensible example 🙏

=> More informations about this toot | More toots from awoerner@mastodon.social

Written by Justin Purnell on 2024-08-15 at 10:49

@mattiem Bless you, @mattiem

=> More informations about this toot | More toots from jpurnell@mastodon.social

Written by Dachary on 2024-08-15 at 19:46

@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

Written by Matt Massicotte on 2024-08-15 at 20:42

@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

Written by Huh, that's weird. on 2024-08-16 at 12:32

@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

Written by Matt Massicotte on 2024-08-16 at 12:41

@noworkie Thank you so much!!

=> More informations about this toot | More toots from mattiem@mastodon.social

Written by Ryan Lintott on 2024-08-16 at 18:23

@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

Written by Matt Massicotte on 2024-08-16 at 18:31

@ryanlintott 💪🏻

=> More informations about this toot | More toots from mattiem@mastodon.social

Written by Juan Arzola on 2024-08-16 at 19:35

@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()

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/112960443815180099
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
571.633313 milliseconds
Gemini-to-HTML Time
16.846095 milliseconds

This content has been proxied by September (3851b).