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")
}
}