@Published, can I force writes on @MainThread - but allow reads from anywhere?

I have a progress indicator

@Published var progress:Float

I read elsewhere that @Published is thread-safe, so it should be fine for me to read the value from any thread.

Of course, I only want to send writes on @MainActor as they drive the UI.

Q1) Am I right in thinking that I'm safe to read from any thread - provided I'm careful to write only on @MainActor
(Are there any deadlock dangers I should be aware of?)

Q2) Is there any good approach to enforce this?

//conceptually...
@MainActor(set)
@Published var progress:Float

I guess a macro which writes creates a setter/getter could be a solution when I can move to XCode 15...

I believe the best practice would be to subscribe on the main thread, and publish from wherever is convenient.

1 Like

I don't think it's safe, if I do that in a simple test app the thread sanitiser complains about data races.

1 Like