Question about @MainActor for `values`:
mortenbekditlevsen opened this issue · comments
Hi there,
This is not actually an issue, but I hope that it is ok to use this for asking a question.
I am playing with the new concurrency features. It works really well 'high up' in the stack, like in view models and so on.
It's excellent to mark your viewmodel as @MainActor
and then have all code after 'awaits' run back on the main thread.
This allows me to remove some worry about adding observe(on:)
to observe on the main scheduler on API boundaries.
My next experiment is to replace some of my existing logic further down in the stack with the concurrency features.
As of yet we can't do something like flatMapLatest
on an AsyncSequence
, so I figured that my mindset should likely change to build this with a nested for await
loop with the inner loop being run inside a Task
so that the outer loop can cancel previous inner loop tasks.
Here I ran into the issue that the .values
extension is marked as @MainActor
. This is perfect for when I used it from a view model, but for the next use I actually intend to be on some 'worker thread'.
TL;DR
My question is: Is the @MainActor
annotation actually necessary for the values
property - or is it rather a hint that you mainly designed the API to be consumed from something like a view model?
Hey Morten !
The @MainActor
annotation only applies to Driver and Signal - since those are guaranteed to emit on the MainScheduler (it's the async-await alternative of observe(on: MainScheduler.instance)
You could theoretically do asObservable() or asInfallible() and those won't have MainActor on them IIRC.
Thank you so much, @freak4pc ! I'll have a look at those.