Xcode Preview Causing Crashes

Building in visionOS and one of my Swift UI views keeps causing Xcode to crash. The root issue is within the preview code, stuck on updating the preview code to prevent Xcode from crashing. When I run the simulator the app works perfectly, no bugs or issues. Any advice on how to update the preview code would be very helpful :)

import RealityKit
import RealityKitContent

struct BrandImage: View {
    @State private var currentIndex: Int = 0
    @Environment(\.openWindow) private var openWindow
    @EnvironmentObject var sharedAppState: SharedAppState
    
    var brand: [BrandEcommData]
    var initialBrand: BrandEcommData
    
    init(brand: [BrandEcommData], initialBrand: BrandEcommData) {
        self.brand = brand
        self.initialBrand = initialBrand
        if let initialIndex = brand.firstIndex(where: { $0.id == initialBrand.id}) {
            _currentIndex = State(initialValue: initialIndex)
        }
    }
    
    var body: some View {
        HStack(spacing: 0) {
            ZStack {
                ForEach(0..<brand.count, id: \.self) { index in
                    if index == currentIndex {
                        VStack {
                            Text(brand[index].brand)
                                .padding(.top, 5)
                            brand[index].image
                                .resizable()
                                .scaledToFit()
                        }
                        .transition(.scale)
                    }
                }
                
                HStack {
                    Button(action: {
                        withAnimation {
                            self.currentIndex = (self.currentIndex - 1 + brand.count) % brand.count
                            sharedAppState.currentModelId = brand[currentIndex].id
                        }
                    }) {
                        Image(systemName: "arrow.left.circle.fill")
                            .font(.largeTitle)
                            .foregroundStyle(.linearGradient(
                                colors: [.black, .gray],
                                startPoint: .top,
                                endPoint: .bottom))
                    }
                    .padding(.leading, 20)
                    
                    Spacer()
                    
                    Button(action: {
                        withAnimation {
                            self.currentIndex = (self.currentIndex + 1) % brand.count
                            sharedAppState.currentModelId = brand[currentIndex].id
                        }
                    }) {
                        Image(systemName: "arrow.right.circle.fill")
                            .font(.largeTitle)
                            .foregroundStyle(.linearGradient(
                                colors: [.black, .gray],
                                startPoint: .top,
                                endPoint: .bottom))
                    }
                    .padding(.trailing, 20)
                }
                
                VStack {
                    HStack {
                        Spacer()
                        Button(action: {
                            openWindow(id: "volumetric")
                        })
                         {
                            Image(systemName: "cube.transparent")
                                .font(.title)
                                .padding()
                                .foregroundStyle(.linearGradient(
                                    colors: [.black, .gray],
                                    startPoint: .top,
                                    endPoint: .bottom))
                        }
                    }
                    Spacer()
                }
            }
            .frame(maxWidth: .infinity)
            Rectangle()
                .frame(width: 2)
                .foregroundStyle(.linearGradient(
                    colors: [.black, .gray],
                    startPoint: .top,
                    endPoint: .bottom))
                
            VStack {
                Text(brand[currentIndex].name)
                    .font(.title2)
                Text(brand[currentIndex].itemDetail)
                    .font(.subheadline)
                Text(brand[currentIndex].itemDescription)
                    .padding()
                Text(brand[currentIndex].price)
            }
        }
        .onAppear {
            sharedAppState.currentModelId = initialBrand.id
        }
    }
}

#Preview {
    if let initialBrand = ecommdata?.first {
        BrandImage(brand: ecommdata!, initialBrand: initialBrand)
    } else {
        Text("Unable to load 3D Asset")
    }
}
Answered by meta_merritt in 781397022

Figured it out using the below :)

    Group {
        if let initialBrand = ecommdata?.first {
            let sharedAppState = SharedAppState(initialModelId: initialBrand.id)
            BrandImage(brand: ecommdata!, initialBrand: initialBrand)
                .environmentObject(sharedAppState)
        } else {
            Text("Unable to load 3D Asset")
        }
    }
}

It would help to post the crash error you are getting, but one thing stands out to me.

@EnvironmentObject var sharedAppState: SharedAppState

Your #Preview code does not inject an environmentObject() instance of that shared app state. The crash report would mention if an environment object was missing. Is that what you see?

@Developer Tools Engineer Looks like from the crash report Thread 0 caused Xcode to crash. Assuming that's aka to include environmentObject() in the preview?

Accepted Answer

Figured it out using the below :)

    Group {
        if let initialBrand = ecommdata?.first {
            let sharedAppState = SharedAppState(initialModelId: initialBrand.id)
            BrandImage(brand: ecommdata!, initialBrand: initialBrand)
                .environmentObject(sharedAppState)
        } else {
            Text("Unable to load 3D Asset")
        }
    }
}

Yup that's exactly it! For what it's worth, sharing the text of the entire crash report is more useful than a portion visible in a screenshot. There's a lot of extra details that can help identify other kinds of issues if need be.

Xcode Preview Causing Crashes
 
 
Q