I have found two possible solutions :
either (1) use @preconcurrency import Translation instead of just import Translation, and it works magically ;
or (2) mark the translationTask closure as @Sendable, and detach to MainActor when needed :
.translationTask(configuration) { @Sendable session in
do {
let responseText = try await session.translate(sourceText).targetText
await MainActor.run {
// ... propagate back to UI
}
} catch {}
}
I'm not sure which is the best solution: #1 is shorter and cleaner but I don't know how it handles concurrency behind the scenes, #2 is more tedious but the isolation path is more clear.
Topic:
Machine Learning & AI
SubTopic:
General
Tags: