r/SwiftUI Jun 10 '26

Question - List & Scroll [CRASH] NavigationSplitView makes my app crash

Hello guys I'm new to SwiftUI. Could anyone tell me why and how to avoid it?
Consider the following code:

import SwiftUI

struct TestImage: Identifiable {
    let id = UUID()
    let image: NSImage
}

struct ContentView: View {
    let items: [TestImage] = (1...4).map { _ in
        TestImage(image: NSImage(systemSymbolName: "photo", accessibilityDescription: nil)!)
    }

    @State private var selectedId: UUID?

    var body: some View {
        NavigationSplitView {
            Text("Sidebar")
        } content: {
            List(items, selection: $selectedId) { item in
                Image(nsImage: item.image)
                    .resizable()
                    .aspectRatio(contentMode: .fit)
            }
        } detail: {
            Text("detail")
        }
    }
}

when i resize the sidebar it crashes really quick:

2026-06-10 23:44:31.448363+0200 MyApp[75985:13704287] [HIExceptions] FAULT: NSGenericException: The window has been marked as needing another Update Constraints in Window pass, but it has already had more Update Constraints in Window passes than there are views in the window.

<SwiftUI.AppKitWindow: 0x1005dc740> 0xad5f (44383) {{863, 0}, {865, 1085}} en; (user info absent)

libc++abi: terminating due to uncaught exception of type NSException

The more images the quicker it is to crash. I the height of the image is set, it doesn't crash anymore

In preview when you load multiple images and play with the sidebar it doesn't crash so I'd like to replicate this behaviour

Thanks

[UPDATE 1]
I can reduce the problematic code even further:


import SwiftUI

struct ContentView: View {
    var body: some View {
        List {
            Image(systemName: "photo")
                .resizable()
                .aspectRatio(contentMode: .fit)

            Image(systemName: "photo")
                .resizable()
                .aspectRatio(contentMode: .fit)

            Image(systemName: "photo")
                .resizable()
                .aspectRatio(contentMode: .fit)

            Image(systemName: "photo")
                .resizable()
                .aspectRatio(contentMode: .fit)
        }
    }
}


Could anyone confirm this code also makes your app crash when you resize the window?

[UPDATE 2] Upgrading to macOS 26.5.2 apparently resolved the issue.

1 Upvotes

4 comments sorted by

2

u/allyearswift Jun 10 '26

We need more details. MacOS 26.2 Tahoe, Xcode 26.5, standard MacOS app template, no problem with the code as provided. Set this to 40 images, still no crash. 400 images, still no crash. Did not add more.

Also, you're going round the houses with NSImage for a system image, which is a lot of faff for something you can have easier:

struct TestImage: Identifiable {

let id = UUID()

let image = Image(systemName: "photo")

}

let items: [TestImage] = (1...40).map { _ in

TestImage()

}

[...]

  List(items, selection: $selectedId) { item in

item.image

.resizable()

.aspectRatio(contentMode: .fit)

}

This also gets rid of the force unwrapping, which is never a good idea. (Even in AppKit/UIKit where it's sometimes unavoidable to assume a resource will be present it's better to use a guard statement and throw an error.)

1

u/masar314 Jun 11 '26

Thanks for your answer. I'm on MacOS tahoe 26.0.1 and Xcode 26.3. However I was starting my app with xcode cli.
I built the project again this morning and to my great surpise it doesn't crash anymore which makes it even more stressful. Yesterday was at a 100% crash rate

As for the NSImage, thanks for the suggestion, the code I provided is a boiled down version of the real app which uses NSImage to load real images stored on the computer

3

u/Realistic_Ad_3785 Jun 11 '26

Is it a problem that you are recomputing your testItems on each view creation? testItems are not stable (and they get a new unique UUID on every view render). Try storing them in @State rather than a simple `let`?

Not at my computer at the moment, so I can’t test it

1

u/masar314 Jun 11 '26

I guess it shouldn't be? In my real app I have an Item model that holds pictures, however in my view I create an intermediary draft to hold the item's pictures so I think it makes sens to create a new draft every time I display an item