How to track down "precondition failure: attribute failed to set an initial value: ???" errors?

Hello,


I recently see sometimes crashing errors with statements like "precondition failure: attribute failed to set an initial value: 103" logged to the console. The numbers at the end vary. Has anybody found a good way (or any way besides blindly trying to change things ;-) to track down those errors?


Cheers, Michael

Accepted Reply

Replies

Hi Claude,


thanks the links helped to solve(?) workaround(?) the issue. I'll add a minimalist example below for reference, if somebody else stumbles across this (or similar) issues. Seems like a bug to me... (FB7581633)


import SwiftUI

struct ContentView: View {
  var body: some View {
    NavigationView {
      List {
        NavigationLink(destination: Detail()) {
          Text("Tap here")
        }
      }
    }
    .navigationViewStyle(StackNavigationViewStyle())
  }
}

struct Detail: View {
  var body: some View {
    NavigationView { // comment out to let the app crash when navigating in portrait orientation
      GeometryReader { geo in
        if geo.size.height > geo.size.width {
          self.portrait
            .frame(width: geo.size.width, height: geo.size.height)
        } else {
          self.landscape
            .frame(width: geo.size.width, height: geo.size.height)
        }
      }
    }
    .navigationBarTitle(LocalizedStringKey("Edit entry"))
  }

  var portrait: some View {
    VStack { // won't crash if this was an HStack, even if geometry reader is not wrapped in an NavigationView
      Text("This is portrait. Crashes if Geometry reader is not wrapped in a NavigationView.")
    }
  }

  var landscape: some View {
    HStack {
      Text("This is landscape. Works.")
    }
  }
}


Cheers, Michael

Many thanks for the feedback. I feel we need to support each other a lot with SwiftUI, because of the scarce documentation and the present 'limits' of SwiftUI.


Good continuation.

Hi, I've recently been getting precondition failure: invalid input index: 2 errors too after the latest Xcode 11.4 update. I use GeometryReader to read the user's Watch screen size so i can draw a frame that is relative in size. Unfortunately NavigationView is not available on WatchOS so I'm not sure what other solutions are available. Do you have any ideas? Thanks.

you give back differend views. try to wrap them up in AnyView( ... ) (is also called type erased wrapper 😉

be caution, it's performance intense


https://developer.apple.com/documentation/swiftui/anyview


DWD

Hi, I currently running into the same issue working on a WatchOS app. Did you manage to find a working solution?

No luck, and haven't heard anything back on a seperate thread I started. I have filed an issue with the Feedback Assistant and hopefully we will get a response from Apple.

This is terrible. My app, in production, is crashing because of this error that was not present in previous versions. I really need a fix, fast.

It seems there are numerous possible causes for this error (which is indicated by various error codes under precondition failure... I was able to eliminate one cause: NavigationView should be inside the GeometryReader, not outside. So if possible, wrap the GeometryReader around the top level view wherever this is possible. Of course, this is no fix for crashed under WatchOS because NavigationView is not available.