Testing Effect.run containing Task #1390
-
HI. I got stuck with testing store in case if action returns Effect.run containing Task. As there is no LongLivingEffect
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@dpliushchaiIOS So instead, do the work directly in the task. If you want to parallelize the work done for each item in the loop, you can use a task group, which stays in the structural concurrency world: case .optimizeCars:
return .run { [cars = state.uploaderModel.cars] send in
await withTaskGroup(of: Void.self) { group in
for car in cars {
group.addTask {
do {
let result = try await environment.optimizer.optimize(
car: car
)
await send(.carOptimizedSuccess(optimizationResult))
} catch {
await send(.carOptimizedFailure(error))
}
}
}
}
} |
Beta Was this translation helpful? Give feedback.
@dpliushchaiIOS
Effect.run
provides an asynchronous context for you to do async work in, butTask
leaves the world of structured concurrency (unless youawait
the result explicitly, set up cancellation handlers, etc.).So instead, do the work directly in the task. If you want to parallelize the work done for each item in the loop, you can use a task group, which stays in the structural concurrency world: