I've got a Swift 6 strict concurrency question. I'm trying to figure out how to pass a large amount of non-Sendable data between actors without making copies and without making the data Sendable (and without using any of the "unchecked"/"unsafe" escape hatches). Here's the situation in my toy example app:
I've got a @MainActor
class AppState
and a Worker
actor. From AppState
I call Worker
's generateProducts()
method. The Worker
makes a big array of non-Sendable WorkProduct
objects and then returns them.
Here's the code where AppState
calls Worker
to generate the products:
And here's the code for Worker
's generateProducts()
method:
This compiles and works in Swift 6 language mode. Yay! I've got one actor
making a bunch of non-Sendable things and returning them to a @MainActor
class! Success!
Later, I want my AppState
object to pass the array of non-Sendable WorkProduct
objects back to the Worker
actor for processing, and then have the Worker
return the processed WorkProduct
objects to AppState
.
Here's what I've tried so far, none of which works:
I feel like what I'm doing should be possible—without making WorkProduct
Sendable. I guess I'm searching for some equivalent of the borrowing
/consuming
stuff from non-copyable types, maybe?
AppState
and Worker
never need to have the big array of non-Sendable WorkProduct
objects at the same time. Either one has it or the other one has it. It should never be shared.
It seems like it should be possible to pass this big chunk of data back and forth without making copies (and therefore keeping the RAM footprint from getting bigger than is needed for a single copy of the array).
Anyone have any advice or ideas?