Quantcast
Channel: Active questions tagged crash - Stack Overflow
Viewing all articles
Browse latest Browse all 7204

SwiftUI app freeze with 100% CPU when canceling back swipe

$
0
0

The following code can make the running app unresponsive on iPhone (not iPad) or iPhone simulator. Xcode shows that the app consumes 100% CPU while allocating more and more memory.

struct SecondView: View {    @State private var keyboardHeight: CGFloat = 0    private let showPublisher = NotificationCenter.Publisher.init(        center: .default,        name: UIResponder.keyboardWillShowNotification    ).map { (notification) -> CGFloat in        if let rect = notification.userInfo?["UIKeyboardFrameEndUserInfoKey"] as? CGRect {            return rect.size.height        } else {            return 0        }    }    var body: some View {        VStack(spacing: 20) {            if keyboardHeight == 0 {                Text("This is shown as long as there's no keyboard")            }            Text("This is the SecondView. Drag from the left edge to navigate back, but don't complete the gesture: crash results.")        }.onReceive(self.showPublisher) { (height) in            self.keyboardHeight = height        }        .navigationBarItems(trailing: Button("Dummy") {  })    }}struct ContentView: View {    @State var textInput = ""    var body: some View {        NavigationView {            VStack(spacing: 20) {                TextField("1. Tap here to show keyboard", text: self.$textInput)                    .textFieldStyle(RoundedBorderTextFieldStyle())                NavigationLink(destination: SecondView()) {                    Text("2. Go to second screen")                }                Spacer()            }        }    }}

To trigger the freeze:

  1. Tap the textfield to make the keyboard appear
  2. Tap the link to go to the next screen
  3. Drag from the left side of the screen, but don't complete the gesture and instead release early

There are some workarounds:

  • Remove the navigation bar item (the Dummy button) in SecondView
  • Remove the use of the keyboardHeight variable in SecondView
  • Don't activate the keyboard in ContentView before navigating

However, I can't use the above workarounds in my app. Does anyone know what the root cause is?


Viewing all articles
Browse latest Browse all 7204

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>