From cf63384dce2f365165f553f4ef1c861b3375b75c Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 25 Sep 2020 11:31:53 -0400 Subject: [PATCH] Why use many DispatchQueue.main.async when few do trick? --- Tusker/Screens/Compose/MainComposeTextView.swift | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Tusker/Screens/Compose/MainComposeTextView.swift b/Tusker/Screens/Compose/MainComposeTextView.swift index a65bd626..7ec9a423 100644 --- a/Tusker/Screens/Compose/MainComposeTextView.swift +++ b/Tusker/Screens/Compose/MainComposeTextView.swift @@ -149,20 +149,18 @@ struct MainComposeWrappedTextView: UIViewRepresentable { context.coordinator.didChange = textDidChange context.coordinator.uiState = uiState - if becomeFirstResponder { - DispatchQueue.main.async { + // wait until the next runloop iteration so that SwiftUI view updates have finished and + // the text view knows its new content size + DispatchQueue.main.async { + self.textDidChange(uiView) + + if becomeFirstResponder { // calling becomeFirstResponder during the SwiftUI update causes a crash on iOS 13 uiView.becomeFirstResponder() // can't update @State vars during the SwiftUI update becomeFirstResponder = false } } - - // wait until the next runloop iteration so that SwiftUI view updates have finished and - // the text view knows its new content size - DispatchQueue.main.async { - self.textDidChange(uiView) - } } func makeCoordinator() -> Coordinator {