# Codable, SwiftConcurrency, Swift 6 - Fundamentally incompatible?

**URL:** <https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114>\
**Category:** Using Swift\
**Created:** [July 11, 2024, 4:34pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114 "2024-07-11T16:34:12Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![tcldr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tcldr/32/24405_2.png) [@tcldr](https://forums.swift.org/u/tcldr)\
**Post date:** [July 20, 2024, 12:30pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/62 "2024-07-20T12:30:05Z")

</div>

> [@vns](#):
>
> But as long as class itself contains only sync methods. If you pass non-`Sendable` to, say, view controller to be stored as a property then, and try to call async method on it, then lack of isolation would still be an issue.

If I understand the original post correctly, the only reason there’s main actor annotated members is because they’re intended to be accessed by the main actor isolated view.

> [@ConfusedVorlon](#):
>
> Some of the properties power UI, so they're @MainActor

So, if you remove these annotations, initialise the class in the background and then transfer the resulting object using RBI and a transferring closure, you can then assign the class to a property within the main actor isolated view and use synchronously.

---

<div class="post-metadata">

**Author:** ![vns](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vns/32/37396_2.png) [@vns](https://forums.swift.org/u/vns)\
**Post date:** [July 20, 2024, 12:30pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/63 "2024-07-20T12:30:20Z")

</div>

Oh, I keep forgetting on that one. It definitely helps in certain cases (and in library code). Yet I wouldn’t go with it as main solution for model types as if you have to put isolation on most of the methods in such type, you probably be better with it isolated as whole.

---

<div class="post-metadata">

**Author:** ![ConfusedVorlon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/confusedvorlon/32/14854_2.png) [@ConfusedVorlon](https://forums.swift.org/u/ConfusedVorlon)\
**Post date:** [July 20, 2024, 12:43pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/64 "2024-07-20T12:43:59Z")

</div>

> [@tcldr](#):
>
> Maybe region based isolation plus `transferring` closures will be the ointment here.

> [@mattie](#):
>
> Yes, that's right! This is a mis-match between the protocol and type isolation.
> 
> But here's what I'm getting at. Why do you have some properties MainActor? It sounds like this class is being constructed in the background, transferred (via RBI or an explicit `sending` return value) to the MainActor, where it then will remain.

Honestly - I think you are all over-thinking this stuff.

Now, perhaps Swift is forcing you to work in these (imho) over-engineered ways. And that's my problem with the language as it stands.

Here are the reasons for me wanting to use @MainActor and Codable:

1. Using codable  
I want to save my state. Codable is fantastic for this.  
Apple describes it as "Swift's native serialization method" for good reason.

2. Using @MainActor  
My class interacts with (is updated by and read from) background operations - and also stores state which drives the UI.

Marking UI properties @MainActor is a fantastic way to (mostly) get the compiler to check that I meet the requirement to only update UI properties on the Main thread.

This stuff isn't complex. It shouldn't be controversial. These are entirely vanilla issues faced by swift users all the time. I'd go as far as to suggest that the main purpose of concurrency for most people is to move expensive work off the main thread - so it can then update the UI when it is done.

My problem is that "Swift's native serialization method" doesn't work with concurrency - and I want both. I don't think that's unreasonable.

When you suggest that I could do something complex with transferring closures then that sounds like pain.

> [@tcldr](#):
>
> So, if you remove these annotations, initialise the class in the background and then transfer the resulting object using RBI and a transferring closure, you can then assign the class to a property within the main actor isolated view and use synchronously.

This is only one step away from - just ignore concurrency. Go back to the old ways, and make sure you (manually!) remember only to make changes on the main thread!

When you suggest that I could redesign my app so that I don't need to mark my properties with @MainActor because I ensure by design that I only act on @MainActor - then that seems risible. We all know how good coders (certainly me) are at guaranteeing never to access data from the wrong context manually. The entire premise of modern concurrency is that we're bad at it and the compiler is better.

Swift has given us @MainActor. I'm going to use it. Marking UI properties is about the most obvious use-case I can think of.  
I expect other Swift stuff to deal with that!

---

<div class="post-metadata">

**Author:** ![ConfusedVorlon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/confusedvorlon/32/14854_2.png) [@ConfusedVorlon](https://forums.swift.org/u/ConfusedVorlon)\
**Post date:** [July 20, 2024, 12:52pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/65 "2024-07-20T12:52:09Z")

</div>

> [@itaiferber](#):
>
> This isn't valid so long as the type isn't `Sendable`, which is part of the problem you're running into here.

There is nothing dangerous about initialising on one thread, then passing the object to another thread.

No danger of race conditions or other concurrency fails providing the sender doesn't use (or implicitly change) the object after passing it off.

Indeed - swift is starting to reason about these cases in the compiler.

But that's not the issue I'm hitting anyway. Sendable wouldn't help.

 ![Screenshot 2024-07-20 at 13.51.46](https://global.discourse-cdn.com/swift/original/3X/c/8/c8221b5faa9e0678bdb9ee9240b876c97a327eff.png)

---

<div class="post-metadata">

**Author:** ![itaiferber](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/itaiferber/32/33840_2.png) [@itaiferber](https://forums.swift.org/u/itaiferber)\
**Post date:** [July 20, 2024, 12:59pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/66 "2024-07-20T12:59:47Z")

</div>

> [@ConfusedVorlon](#):
>
> No danger of race conditions or other concurrency fails providing the sender doesn't use (or implicitly change) the object after passing it off.

Except that's not the semantic promise you're making to the compiler. By not marking your type as `Sendable`, you're telling the compiler that there _is_ a danger of race conditions were a value of this type being transferred between isolation regions (/threads/whatever), and that you promise not to do it or else you invoke undefined behavior.

Making this promise to the compiler is spelled `Type: Sendable`.

> [@ConfusedVorlon](#):
>
> But that's not the issue I'm hitting anyway. Sendable wouldn't help.

That's because you're still marking your type as `@MainActor`, which means "the properties on this type are only safe to touch on the main actor; you can pass this object around, but only read from it on the main actor" (which means that `encode(to:)` could only ever be called on the main actor, which you can't promise).

If this object really is a bog-standard data object, you can just write

```swift
class Foo: @unchecked Sendable, Codable {
    var bar: Int?
}

```

and get everything you want.

---

<div class="post-metadata">

**Author:** ![ConfusedVorlon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/confusedvorlon/32/14854_2.png) [@ConfusedVorlon](https://forums.swift.org/u/ConfusedVorlon)\
**Post date:** [July 20, 2024, 1:06pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/67 "2024-07-20T13:06:57Z")

</div>

Imaginary Apple announcement

> Swift brought native serialization with Codable, and compiler safe concurrency.
> 
> With Swift 6 you can get the ergonomics of Codable as well as the safety of modern concurrency.  
> AsyncCodable now provides all the convenience of Codable - but will operate and generate conformances on the appropriate isolation region for your class, struct or actor.
> 
> Most of the time, the compiler can figure out what isolation region it should generate conformance on - and the encoder or decoder will access that asynchronously.
> 
> In cases where you have mixed isolation, then you can manually define async encode(to: or decode(from:

---

<div class="post-metadata">

**Author:** ![ConfusedVorlon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/confusedvorlon/32/14854_2.png) [@ConfusedVorlon](https://forums.swift.org/u/ConfusedVorlon)\
**Post date:** [July 20, 2024, 1:21pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/68 "2024-07-20T13:21:27Z")

</div>

> [@itaiferber](#):
>
> That's because you're still marking your type as `@MainActor`, which means "the properties on this type are only safe to touch on the main actor; you can pass this object around, but only read from it on the main actor" (which means that `encode(to:)` could only ever be called on the main actor, which you can't promise).

This is EXACTLY my point.

If you want to use "Swift's native serialization system", then you can't also use SwiftConcurrency.

To me, that's an epic fail.

I understand why the language as it stands fails so hard here - but it seems absurd to me that Apple is pushing this language mode with such a crazy gap in functionality.

- Ergonomic Serialization
- Safe Concurency

Pick One 🤣 🤦‍♂️ 🤯

The reason I initially posted this thread was because I kinda assumed I had to be missing something. Surely Apple wouldn't be pushing us towards something so half-assed?

I have learned a few handy modifiers in this thread to 'YOLO' my way around the warnings - but clearly 'pick one' is the current state of play.

---

<div class="post-metadata">

**Author:** ![tcldr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tcldr/32/24405_2.png) [@tcldr](https://forums.swift.org/u/tcldr)\
**Post date:** [July 20, 2024, 1:39pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/69 "2024-07-20T13:39:13Z")

</div>

> [@ConfusedVorlon](#):
>
> If you want to use "Swift's native serialization system", then you can't also use SwiftConcurrency.

This isn't true.

It's totally possible to initialise any non isolated type in any isolation context. That type can conform to any non isolated protocol (such as Codable, Hashable, Equatable, Sequence and many 100s of others). With a tiny bit of boilerplate (actor isolated wrapper) you can transfer that instance across an isolation context today. And in the near future [that will be made simpler](https://forums.swift.org/t/how-can-i-use-region-based-isolation/71426/5).

The fact is, a non isolated class with mutable members isn't thread safe. Marking a couple of members with `@MainActor` doesn't make it so. In fact, it just fragments its members across isolation domains and forces your asynchronous work back to the main thread – which is surely the situation you were trying to avoid by initialising it asynchronously in the first place.

EDIT: Turns out the near future is closer than I thought. [It's available in Swift 6](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/74).

---

<div class="post-metadata">

**Author:** ![vns](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vns/32/37396_2.png) [@vns](https://forums.swift.org/u/vns)\
**Post date:** [July 20, 2024, 1:41pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/70 "2024-07-20T13:41:47Z")

</div>

> [@ConfusedVorlon](#):
>
> Swift has given us @MainActor. I'm going to use it. Marking UI properties is about the most obvious use-case I can think of.

That’s not what @tcldr suggesting. The suggestion is to create instance (non-isolated) in the background, then transfer it to the main actor context. All ensured by compiler, not loosing of any benefits. Using `MainActor` everywhere, without considerations on isolation boundaries design is not what Swift aims for. Solutions here are aimed to keep benefits of this compiler checks, not to suppress them.

---

<div class="post-metadata">

**Author:** ![mattie](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mattie/32/13267_2.png) [@mattie](https://forums.swift.org/u/mattie)\
**Post date:** [July 20, 2024, 1:56pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/71 "2024-07-20T13:56:08Z")

</div>

> [@ConfusedVorlon](#):
>
> Using @MainActor  
> My class interacts with (is updated by and read from) background operations - and also stores state which drives the UI.

I want to stress this, because I think it is the core of why people are struggling. It definitely is for me.

Swift concurrency does not support this arrangement without unsafe opt-outs. A _Swift 6 compiler_ can sometimes accomidate it via region-based isolation. And further, I _think_ all of the situations were this could work would remain valid if the MainActor isolation were removed.

---

<div class="post-metadata">

**Author:** ![ConfusedVorlon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/confusedvorlon/32/14854_2.png) [@ConfusedVorlon](https://forums.swift.org/u/ConfusedVorlon)\
**Post date:** [July 20, 2024, 2:11pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/72 "2024-07-20T14:11:49Z")

</div>

> [@mattie](#):
>
> > [@ConfusedVorlon](#):
> >
> > Using @MainActor  
> > My class interacts with (is updated by and read from) background operations - and also stores state which drives the UI.
> 
> ...
> 
> Swift concurrency does not support this arrangement without unsafe opt-outs. ...

I may be missing your point - but to me, this is the core of what modern concurrency supports.

from some code that isn't guaranteed to be Mainactor, use async methods and the following:

read with  
`let foo = await instance.mainActorProperty`

write with

```swift
await MainActor.run {
instance.mainActorProperty = newValue
}

```

am I misunderstanding your point?

---

<div class="post-metadata">

**Author:** ![tcldr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tcldr/32/24405_2.png) [@tcldr](https://forums.swift.org/u/tcldr)\
**Post date:** [July 20, 2024, 2:18pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/73 "2024-07-20T14:18:55Z")

</div>

> [@mattie](#):
>
> Swift concurrency does not support this arrangement without unsafe opt-outs. A _Swift 6 compiler_ can sometimes accomidate it via region-based isolation.

As a point of interest, rather than a recommendation, I believe if you _really_ want to avoid the unsafe opt-out, you can use a wrapping type that will permit the transfer.

```swift
class NonSendable {}

@MainActor struct Wrapper {
  
  let ns: NonSendable
  
  init(_ ns: NonSendable) {
    self.ns = ns
  }
}

func createInBackground(_ completion: @escaping @MainActor (Wrapper) -> Void) async {
  let ns = NonSendable()
  let wrapper = await Wrapper(ns)
  await completion(wrapper)
}

```

But in the meantime I'm happy with the unsafe opt-outs:

```swift
func createInBackground(_ completion: @escaping @MainActor (NonSendable) -> Void) async {
  nonisolated(unsafe) let ns = NonSendable()
  await completion(ns)
}

```

Until finally we get transferring:

```swift
// note additional closure annotation
func createInBackground(_ completion: @transferring @escaping @MainActor (NonSendable) -> Void) async {
  let ns = NonSendable()
  await completion(ns)
}

```

---

<div class="post-metadata">

**Author:** ![mattie](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mattie/32/13267_2.png) [@mattie](https://forums.swift.org/u/mattie)\
**Post date:** [July 20, 2024, 2:30pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/74 "2024-07-20T14:30:29Z")

</div>

The `sending` keyword is available in Swift 6 and it is great!

> <https://github.com/swiftlang/swift-evolution/blob/main/proposals/0430-transferring-parameters-and-results.md>

---

<div class="post-metadata">

**Author:** ![tcldr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tcldr/32/24405_2.png) [@tcldr](https://forums.swift.org/u/tcldr)\
**Post date:** [July 20, 2024, 2:33pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/75 "2024-07-20T14:33:45Z")

</div>

So it is! Thanks!

So really, background initialisation of a non sendable type isn't harder than marking a parameter as `sendable`:

```swift
nonisolated func createInBackground(
  _ completion: @escaping @MainActor (sending NonSendable) -> Void
) async {
  let ns = NonSendable()
  await completion(ns)
}

```

---

<div class="post-metadata">

**Author:** ![mattie](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mattie/32/13267_2.png) [@mattie](https://forums.swift.org/u/mattie)\
**Post date:** [July 20, 2024, 2:53pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/76 "2024-07-20T14:53:34Z")

</div>

I think so.

My point is that your non-Sendable `instance` value is _by definition_ only accessible in one isolation domain. The compiler will not allow you to access it from both background operations and the `MainActor`.

```swift
await MainActor.run {
    instance.mainActorProperty = newValue
}

```

For this to be valid, `instance` must either be already on the `MainActor` or possible to transfer to it (a Swift 6 feature). And in _both_ cases, it is not possible to access from a non-`MainActor` context. It is non-`Sendable`.

So, if you really are creating this object in the background and then _exclusively_ accessing it from the `MainActor`, the isolation is both unnecessary and problematic. But if you are later on _accessing_ this class from a background thread, you have a race condition\*. And the compiler should be finding that unless there are some opt-outs being used.

\* as far as the compiler is concerned. I'm not implying you have a real bug!

---

<div class="post-metadata">

**Author:** ![ConfusedVorlon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/confusedvorlon/32/14854_2.png) [@ConfusedVorlon](https://forums.swift.org/u/ConfusedVorlon)\
**Post date:** [July 20, 2024, 3:53pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/77 "2024-07-20T15:53:31Z")

</div>

> [@mattie](#):
>
> So, if you really are creating this object in the background and then _exclusively_ accessing it from the `MainActor`, the isolation is both unnecessary ...

The way I _know_ I'm exclusively accessing it from MainActor is by marking the relevant properties as @MainActor.  
That lets the compiler tell me if I inadvertently do wrong. So, technically - I guess I don't need the annotation as the program runs, but I certainly need it as I develop.  
(and of course - the compiled code isn't annotated - it's just generated so that it _actually_ accesses on the right thread)

> [@mattie](#):
>
> But if you are later on _accessing_ this class from a background thread, you have a race condition\*.

Agreed.

---

<div class="post-metadata">

**Author:** ![vns](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vns/32/37396_2.png) [@vns](https://forums.swift.org/u/vns)\
**Post date:** [July 20, 2024, 4:08pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/78 "2024-07-20T16:08:45Z")

</div>

> [@ConfusedVorlon](#):
>
> The way I _know_ I'm exclusively accessing it from MainActor is by marking the relevant properties as @MainActor.

Considering the initial example:

```swift
class Doc: Codable {
    var foo: String
    @MainActor
    var title: String
}

```

Once you are trying to access `title` in the way you suggest,

```swift
nonisolated func offMain(doc: Doc) async {
    let title = await doc.title // error here
}

```

You'll get the `error: non-sendable type 'Doc' passed in implicitly asynchronous call to main actor-isolated property 'title' cannot cross actor boundary` at the line you are awaiting property, because `Doc` isn't `Sendable` and can't cross boundary. The way you want read/write this main actor isolated property on non-isolated non-`Sendable` type is the source of data races.

---

<div class="post-metadata">

**Author:** ![ConfusedVorlon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/confusedvorlon/32/14854_2.png) [@ConfusedVorlon](https://forums.swift.org/u/ConfusedVorlon)\
**Post date:** [July 20, 2024, 4:27pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/79 "2024-07-20T16:27:00Z")

</div>

> [@vns](#):
>
> You'll get the `error: non-sendable type 'Doc' passed in implicitly asynchronous call to main actor-isolated property 'title' cannot cross actor boundary` at the line you are awaiting property, because `Doc` isn't `Sendable` and can't cross boundary. The way you want read/write this main actor isolated property on non-isolated non-`Sendable` type is the source of data races.

Sure. I get that. And as I get pushed along the swift 6 treadmill, increasingly the compiler will insist that I do this stuff in a provably correct way.

But it feels like we're going down rabbit holes here. I don't currently have any issues with this kind concurrency design.

The thing I _do_ have an issue with is:

> [@ConfusedVorlon](#):
>
> - Ergonomic Serialization
> - Safe Concurency
> 
> Pick One 🤣 🤦‍♂️ 🤯

---

<div class="post-metadata">

**Author:** ![mattie](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mattie/32/13267_2.png) [@mattie](https://forums.swift.org/u/mattie)\
**Post date:** [July 20, 2024, 4:28pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/80 "2024-07-20T16:28:39Z")

</div>

> [@ConfusedVorlon](#):
>
> The way I _know_ I'm exclusively accessing it from MainActor is by marking the relevant properties as @MainActor.  
> That lets the compiler tell me if I inadvertently do wrong. So, technically - I guess I don't need the annotation as the program runs, but I certainly need it as I develop.

Except! There is _no such thing_ as "inadvertently doing it wrong" with Swift's concurency model. The compiler will not allow you to make the mistakes you are worried about. You are not giving up any safety by removing these annotations! This is kind of the magic of non-`Sendable` types.

And I understand why you are struggling with protocols here. A type like this is incompatible with _all_ non-isolated protocols that use synchronous methods, not just `Codable`.

---

<div class="post-metadata">

**Author:** ![ConfusedVorlon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/confusedvorlon/32/14854_2.png) [@ConfusedVorlon](https://forums.swift.org/u/ConfusedVorlon)\
**Post date:** [July 21, 2024, 8:28pm UTC](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114/81 "2024-07-21T20:28:53Z")

</div>

> [@mattie](#):
>
> A type like this is incompatible with _all_ non-isolated protocols that use synchronous methods, not just `Codable`.

Right.

It seems insane to me that the key plumbing of the language hasn't been updated to play nicely with isolation.

[Previous page](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114.md?page=3)

[Next page](https://forums.swift.org/t/codable-swiftconcurrency-swift-6-fundamentally-incompatible/73114.md?page=5)
