tonyarnold,
@tonyarnold@mastodon.social avatar

Swift Concurrency question: I have a network client (class) that is wrapped by an actor (ModelActor for SwiftData) at a higher level, however using the network client as a property of the actor results in Sendable warnings whenever I try to call any of it's methods.

What do I do here? Make the class an actor?

mattiem,
@mattiem@mastodon.social avatar

@tonyarnold Is this my fault?

tonyarnold,
@tonyarnold@mastodon.social avatar

@mattiem not at all. I have this happening in my own code - the rules aren’t as clear as I’d like them to be.

mattiem,
@mattiem@mastodon.social avatar

@tonyarnold But I’ll try to answer regardless. Ideally, you want to remove the boundary crossing, not make the boundary crossing ok. Many networking constructs people make actors, but their state is only accessed to drive a UI. In these cases, I think it is generally a mistake to make them actors and it just makes your life harder.

tonyarnold,
@tonyarnold@mastodon.social avatar

@mattiem and yet, everything in my experience says not to run network or persistence tasks on the UI thread? It seems like some really common activities are incredibly hard to reason about and do now? (Perhaps rightly in the name of safety)

mattiem,
@mattiem@mastodon.social avatar

@tonyarnold But these are async, right? You must not execute long-running synchronous stuff on the main thread.

mattiem,
@mattiem@mastodon.social avatar
tonyarnold,
@tonyarnold@mastodon.social avatar

@mattiem interesting! I’d need to try it in anger, but every instinct I have is saying “not on the main thread!” 😅

mattiem,
@mattiem@mastodon.social avatar

@tonyarnold Your instinct is right! You have to keep the slow, synchronous work off the main thread. Try to separate thinking about work and state! Do you only ever make use of it from the MainActor?

tonyarnold,
@tonyarnold@mastodon.social avatar

@mattiem no, it's actually hidden behind another actor that conforms to ModelActor and marshals the responses from the network into persisted SwiftData model entities.

Most of Apple's samples and examples just interact with SwiftData from the main thread (directly from the SwiftUI View).

mattiem,
@mattiem@mastodon.social avatar

@tonyarnold ok so this might be a use-case for isolated parameters! It depends a little on how everything is hooked up internally, but could work!

jelly,
@jelly@jellystyle.social avatar

@tonyarnold it either needs to be an actor or Sendable. the trick is, if you make it an actor, you can only access its methods and properties from an async context. if it's Sendable, it either needs to be non-mutable, or you need to lock mutations and likely mark it @unchecked

tonyarnold,
@tonyarnold@mastodon.social avatar

@jelly that was what I assumed, so thank you for explaining it so clearly!

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • ethstaker
  • DreamBathrooms
  • GTA5RPClips
  • magazineikmin
  • InstantRegret
  • cubers
  • thenastyranch
  • Youngstown
  • rosin
  • slotface
  • osvaldo12
  • ngwrru68w68
  • kavyap
  • everett
  • megavids
  • Durango
  • normalnudes
  • Leos
  • mdbf
  • khanakhh
  • tester
  • modclub
  • cisconetworking
  • anitta
  • tacticalgear
  • provamag3
  • JUstTest
  • lostlight
  • All magazines