# AsyncPublisher and Sendable

**URL:** <https://forums.swift.org/t/asyncpublisher-and-sendable/65466>\
**Category:** Using Swift\
**Tags:** combine, sendable, asyncsequence\
**Created:** [June 9, 2023, 4:22pm UTC](https://forums.swift.org/t/asyncpublisher-and-sendable/65466 "2023-06-09T16:22:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![tourultimate](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@tourultimate](https://forums.swift.org/u/tourultimate)\
**Post date:** [June 9, 2023, 4:22pm UTC](https://forums.swift.org/t/asyncpublisher-and-sendable/65466/1 "2023-06-09T16:22:54Z")

</div>

As far as I can tell `AsyncPublisher` and `AsyncThrowingPublisher` are not Sendable. I'm trying to address the Sendable warnings in our code when compiling with strict-concurrency.

The aforementioned types do not seem to be Sendable, but it seems to me they will be often used in context's that require them to be Sendable.

For example, I'm capturing a reference to an `AsyncPublisher` in a `Task` closure, and that closure requires the captures to be Sendable.

What are my options in this case?

---

<div class="post-metadata">

**Author:** ![setoelkahfi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/setoelkahfi/32/3471_2.png) [@setoelkahfi](https://forums.swift.org/u/setoelkahfi)\
**Post date:** [January 29, 2024, 10:21am UTC](https://forums.swift.org/t/asyncpublisher-and-sendable/65466/2 "2024-01-29T10:21:23Z")

</div>

Hey @tourultimate , did you find the workaround for this? I'm currently doing what you're doing now and wondering how did you approach this.

---

<div class="post-metadata">

**Author:** ![tourultimate](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@tourultimate](https://forums.swift.org/u/tourultimate)\
**Post date:** [January 29, 2024, 8:00pm UTC](https://forums.swift.org/t/asyncpublisher-and-sendable/65466/3 "2024-01-29T20:00:34Z")

</div>

IIRC, I don't think we were able to fix that warning. The region based isolation might eventually help.

---

<div class="post-metadata">

**Author:** ![mylaluna](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mylaluna/32/27303_2.png) [@mylaluna](https://forums.swift.org/u/mylaluna)\
**Post date:** [April 3, 2024, 9:26am UTC](https://forums.swift.org/t/asyncpublisher-and-sendable/65466/4 "2024-04-03T09:26:00Z")

</div>

I got the same issue here. Passing an AsyncPublisher, which is commonly produced by .values of @Published array, into a function that iterates the values within a task triggers the warning of non-sendable type.

I don't know if there is an elegant solution to this common situation.

---

<div class="post-metadata">

**Author:** ![manmal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/manmal/32/13522_2.png) [@manmal](https://forums.swift.org/u/manmal)\
**Post date:** [December 13, 2024, 8:30am UTC](https://forums.swift.org/t/asyncpublisher-and-sendable/65466/5 "2024-12-13T08:30:40Z")

</div>

For anyone looking for a (temporary) solution:

```swift
@preconcurrency import Combine

```

---

<div class="post-metadata">

**Author:** ![kelin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kelin/32/21678_2.png) [@kelin](https://forums.swift.org/u/kelin)\
**Post date:** [April 2, 2025, 9:15am UTC](https://forums.swift.org/t/asyncpublisher-and-sendable/65466/6 "2025-04-02T09:15:07Z")

</div>

Hi! What will `@preconcurrency` tag do? It probably will suppress warnings, but will it make the code safe? As I understand, Sendable requires object to be actor-safe.

So if I have a class like this:

```swift
@preconcurrency import Combine

final class MyClass: Sendable {
    static let shared = MyClass()

    let publisher: AnyPublisher
}

```

Will it crash my app or not? When I access it from different context.

---

<div class="post-metadata">

**Author:** ![manmal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/manmal/32/13522_2.png) [@manmal](https://forums.swift.org/u/manmal)\
**Post date:** [April 2, 2025, 9:57am UTC](https://forums.swift.org/t/asyncpublisher-and-sendable/65466/7 "2025-04-02T09:57:00Z")

</div>

Yes `@preconcurrency` is a workaround to disable sendability checking for entire frameworks.

We are using `publisher.values` (which creates an AsyncSequence) quite a bit, but after checking, I noticed that we never pass this across isolation boundaries. I don't see `AsyncThrowingPublisher` conforming to Sendable in the docs, so that probably wouldn't work either.

There are certainly workarounds for your situation, but it depends on the constraints and requirements you have.

---

<div class="post-metadata">

**Author:** ![kelin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kelin/32/21678_2.png) [@kelin](https://forums.swift.org/u/kelin)\
**Post date:** [April 2, 2025, 1:42pm UTC](https://forums.swift.org/t/asyncpublisher-and-sendable/65466/8 "2025-04-02T13:42:14Z")

</div>

My easiest workaround would be prefixing `MyClass` with `@MainActor`, however, it's much more interesting to implement `Sendable` so `shared` instance would not produce any warning and the class would be usable from different contexts.

---

<div class="post-metadata">

**Author:** ![manmal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/manmal/32/13522_2.png) [@manmal](https://forums.swift.org/u/manmal)\
**Post date:** [April 2, 2025, 2:30pm UTC](https://forums.swift.org/t/asyncpublisher-and-sendable/65466/9 "2025-04-02T14:30:52Z")

</div>

One approach would be to to abstract away the Publisher by wrapping it in an `AsyncSequence`. Plenty of approaches exist, depending on the requirements. E.g. if there's only ever one subscriber, then an `AsyncStream` suffices, and it offers buffering for free. For situations with multiple subscribers, we have `MutableSharedStream` in our codebase, which I'm unfortunately not at liberty to share.

Or, with some plumbing, you could make `Shared` from swift-sharing [1] work (adjustments needed if you want to mutate synchronously anywhere), which retains the current value. The idea is to start a `Task` that writes into `Shared` (or `MutableSharedStream`), and manage the Task's lifetime accordingly.

1: [GitHub - pointfreeco/swift-sharing: A universal solution to persistence and data sharing in surprisingly little code.](https://github.com/pointfreeco/swift-sharing)

---

<div class="post-metadata">

**Author:** ![kelin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kelin/32/21678_2.png) [@kelin](https://forums.swift.org/u/kelin)\
**Post date:** [April 2, 2025, 4:05pm UTC](https://forums.swift.org/t/asyncpublisher-and-sendable/65466/10 "2025-04-02T16:05:00Z")

</div>

Seems irrelevant. You misconcept the idea of Combine mixing it with AsyncStream.

You think probably I need Combine because I want some data to be delivered asynchronously? It doesn't follows from my example. No, I want Combine because I want its features, including multiple subscribers and all that transformations.

Looks like you correctly understood that I'm speaking about Concurrency problems caused by `shared`. All `static let` properties required to be `Sendable`. And to make it Sendable we need all stored properties to be Sendable, including the publisher.  
Using third party lib for a singleton seems to be a completely overkill for me.

Sorry man, initially you was very helpfull but the last post value dropped. Did you write it with LLM? If so, don't waist your time, I also use LLM, many of them.

---

<div class="post-metadata">

**Author:** ![kelin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kelin/32/21678_2.png) [@kelin](https://forums.swift.org/u/kelin)\
**Post date:** [April 2, 2025, 4:06pm UTC](https://forums.swift.org/t/asyncpublisher-and-sendable/65466/11 "2025-04-02T16:06:40Z")

</div>

Also, if I needed just a single subscriber, I would simply use delegate.

---

<div class="post-metadata">

**Author:** ![manmal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/manmal/32/13522_2.png) [@manmal](https://forums.swift.org/u/manmal)\
**Post date:** [April 2, 2025, 7:22pm UTC](https://forums.swift.org/t/asyncpublisher-and-sendable/65466/12 "2025-04-02T19:22:20Z")

</div>

> [@manmal](#):
>
> es, and it offers buffering for free. For situations with multiple subscribers, we have `MutableSharedStream` in our codebase, which I'm unfortunately not at liberty to share.

AsyncSequence in principle supports "all that transformations", and multiple subscribers needs some workarounds that I've described. My posts are not LLM generated FWIW, but I feel you don't appreciate my answers so - hope you find a solution that suits you.
