Unclear sentence in the documentation #1678
-
Hello :) The documentation (
Are you saying that I think what you meant is that If I'm correct in the above interpretation, may I suggest amending the documentation? Not everyone would bother to read the actual code and some might get confused by the sentence as written. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Hey @wtruppel! Forget about the viewstore.send(.action) This is a sync function. It returns when the reducer finishes reducing print("1")
DispatchQueue.global.async { … }
print("2") This prints "2" immediately, regardless of the duration of what happens in the dispatched block. viewstore.send(.action).finish() This is now an async function that will return when all the effects (including side effects) produced by the reducers are finished. It means that if you trigger a Because of the way Swift concurrency works, if you cancel the In other words, Please note that this You may also directly cancel the .task {
let viewStoreTask = viewStore.send(.action) // We don't await, but an effect may be running.
try await Task.sleep(for: .seconds(1))
await viewStoreTask.cancel() // Manually cancel the effect if it's still ongoing and if this
// line is reached, that is, if the task wasn't cancelled while
// we were sleeping.
// At this stage, we know that the effect were effectively cancelled.
} The So you're getting it right. I simply rephrased it in any case it helps confirming your understanding. I don't know how the documentation can be improved, but I'm aware that there are plans to expand the concurrency section. |
Beta Was this translation helpful? Give feedback.
-
In addition to what @tgrapperon, note that this is the implementation of /// Waits for the task to finish.
public func finish() async {
await self.rawValue?.cancellableValue
} And that extension Task where Failure == Never {
@usableFromInline
var cancellableValue: Success {
get async {
await withTaskCancellationHandler {
await self.value
} onCancel: {
self.cancel()
}
}
}
} So indeed, if the task created by SwiftUI's |
Beta Was this translation helpful? Give feedback.
-
@tgrapperon @mbrandonw |
Beta Was this translation helpful? Give feedback.
Hey @wtruppel! Forget about the
.task
modifier for a second:This is a sync function. It returns when the reducer finishes reducing
.action
. Note however that this doesn't include side-effects like.run
,.task
, and.fireAndForget
that you may have produced, because these ones are not evaluated synchronously. This is similar to what happens withThis prints "2" immediately, regardless of the duration of what happens in the dispatched block.
This is now an async function that will return when all the effects (including side effects) produced by the reducers are finished. It means …