Post

Replies

Boosts

Views

Activity

Fatal error calling TranslationSession.translate(String) in iOS 18
I'm trying to use the iOS 18 translation feature which works fine if I scroll slowly, but run into fatal errors when I scroll quickly. In my view, I have this state property: @State private var translationConfiguration: TranslationSession.Configuration? In the body, I attach a .translationTask closure to the top-level view: .translationTask(translationConfiguration) { translationSession in Task { @MainActor in ... let response = try await translationSession.translate(originalContent) // this line causes the fatal error. } } I also have an onAppear { } closure on the same top-level view, which sets translationConfiguration if it is nil, or calls translationConfiguration?.invalidate() if it's non-nil. When I scroll quickly, the view presumably appears and disappears quickly, but it seems that the translationTask closure still runs after the view disappears. I get this fatal error: Translation/TranslationError+TranslationOnly.swift:54: Fatal error: Attempted to use TranslationSession after the view it was attached to has disappeared, which is not supported. Instead of storing a TranslationSession instance outside of the .translationTask closure, trigger a .translationTask to run again on a visible view and use that TranslationSession instance. The recommendation from the error message seems incorrect, though. I am not using the TranslationSession instance outside of the translationTask closure. I'm using it only inside of it. I tried to keep an extra visible boolean property that gets set to true when the onAppear closure is called, and set to false when the onDisappear closure is called, and then I guard on that within the translationTask, but that doesn't fix the issue. What am I missing?
2
1
259
Oct ’24