NavigationSplitView crashes in Xcode, iOS Beta 7

I have a NavigationSplitView with a sidebar. When selecting a new item on the sidebar, the app crashes. The error message says:

Simultaneous accesses to 0x6000030107f0, but modification requires exclusive access.

Xcode shows that the crash occurs inside the generated code in my class with @Observable macro.

@ObservationIgnored private let _$observationRegistrar = Observation.ObservationRegistrar()

internal nonisolated func access<Member>(
    keyPath: KeyPath<NavModel , Member>
) {
  _$observationRegistrar.access(self, keyPath: keyPath)
}

internal nonisolated func withMutation<Member, MutationResult>(
  keyPath: KeyPath<NavModel , Member>,
  _ mutation: () throws -> MutationResult
) rethrows -> MutationResult {
  // Crash occurs on the following line
  try _$observationRegistrar.withMutation(of: self, keyPath: keyPath, mutation)
}

@ObservationIgnored private var _section: SidebarSection? = .one

To reproduce the crash, I tap a new item on the sidebar until the app crashes. It usually only takes 1-3 times selecting a new item before the crash occurs.

Below is the code for an entire app to reproduce the crash. Has anyone else encountered this issue? Thank you!

import SwiftUI

@main
struct NavigationBugApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

@Observable
class NavModel {
    var section: SidebarSection? = .one
}

enum SidebarSection: Hashable {
    case one
    case two
}

struct ContentView: View {
    @State private var model = NavModel()
    
    var body: some View {
        NavigationSplitView {
            List(selection: $model.section) {
                NavigationLink("One", value: SidebarSection.one)
                NavigationLink("Two", value: SidebarSection.two)
            }
            .listStyle(.sidebar)
        } detail: {
            Text("Hello World")
        }
    }
}

#Preview {
    ContentView()
}
  • I too have the same problem.

    If I change the existing ObservableObject class to @Observable I get the same error when selecting an item from the sidebar.

    After seeing this error it seems @Observable is still unstable.

    And this problem occurs on iPad and works fine on macOS.

Add a Comment

Replies

From the iOS & iPadOS 17 Beta 8 Release Notes:

SwiftUI

Known Issues

  • On iOS, using an Observable object’s property as a selection value of a List inside NavigationSplitView may cause a “Simultaneous accesses to …” error when a list selection is made via tap gesture. (113978783)

    Workaround: There is no current workaround for Observable properties. Alternatives include factoring out the selection value into separate state stored outside the object, or using ObservableObject instead.

Your best bet is to wait for the next beta (or RC) and try again with that. I would only use the workaround if you need a fix right now.

  • Today, I updated XCode 15.0 beta 8 and conducted a test.

    The problem was not resolved in XCode 15.0 beta 8.

    Even if this problem is resolved, using @Observable for the time being seems like a risky choice.

Add a Comment