SwiftUI Crashes only on A-Chip device

Hey there,

I am currently developing a SwiftUI app for iPad.

However, I am currently experiencing a behavior, that I can not explain.

My code creates a user-management view. This all works fine if I run it on my iPad Pro with Apple-M-Chip. However, if I try to run the exact same code on an older A-Chip iPad Air (4th Generation), my app crashes.

I can recreate this behavior in the simulator on my Mac. When I run it on an iPad Pro Simulator, all works fine. When I run it in an iPad Air (4th Gen) simulator, the app becomes unresponsive.

Unfortunatley Xcode does not throw any error that I could use to identify the problem.

Currently I solved it with a work-around: As soon as I add a Spacer (even with a with of 0) the code works as expected and the UI is shown. As soon as I remove the Spacer, it crashes again.

HStack(alignment: .top){
                
                    VStack{
                        if(data.ui_user.id != -1){
                            HStack{
                                Text("Employee-No.:")
                                Spacer()
                                TextField("Employee-No.", value: $data.ui_user.id, format: .number).textFieldStyle(.roundedBorder).disabled(true).containerRelativeFrame(.horizontal, count: 10, span: 3, spacing: 0)
                            }
                        }
                        HStack{
                            Text("Family Name:")
                            Spacer()
                            TextField("Family Name", text: $data.ui_user.familyName).textFieldStyle(.roundedBorder).containerRelativeFrame(.horizontal, count: 10, span: 3, spacing: 0)
                        }
                        HStack{
                            Text("Given Name:")
                            Spacer()
                            TextField("Given Name", text: $data.ui_user.givenName).textFieldStyle(.roundedBorder).containerRelativeFrame(.horizontal, count: 10, span: 3, spacing: 0)
                        }
                        
                        HStack{
                            Text("Role:")
                            Spacer()
                            Picker("Select role", selection: $data.selectedRole){
                                ForEach(0 ..< self.availableRoles.count, id:\.self){
                                    Text(self.availableRoles[$0].name ?? "???").tag($0)
                                }                                
                            }.pickerStyle(.menu).containerRelativeFrame(.horizontal, count: 10, span: 3, spacing: 0).background(.drkBlueContainer).cornerRadius(5).foregroundColor(.drkOnBlueContainer).tint(.drkOnBlueContainer)
                             
                        //If I add this spacer, it works. If not, it crashes
                        //Spacer().frame(width: 0.0)
                        }.disabled(noEditAdmin && data.ui_user.id != -1)
                        
                    }
                Divider()
                
                    VStack{
                        HStack{
                            Text("Created at:")
                            Spacer()
                            TextField("Created at", value: $data.ui_user.createdAt, format: .dateTime).textFieldStyle(.roundedBorder).disabled(true).containerRelativeFrame(.horizontal, count: 10, span: 3, spacing: 0)
                        }
                        HStack{
                            Toggle("Terminates:", isOn: $data.ui_user.expires)
                                .padding(.trailing, 2.0)
                        }
                        if(data.ui_user.expires){
                            HStack{
                                DatePicker("Terminates at:", selection: $data.ui_user.expiresAt, displayedComponents: .date).pickerStyle(.automatic)
                            }}
                    }
            }

If anyone has any ideas, I would be very grateful for a hint :-)

Hi @NilsR ,

hmm, this looks interesting! I tried to replicate this but need a little more info. What iOS are you running on both iPads? What is the minimum your app targets?

I attempted with iOS 17.5 and it did not crash (minimum of iOS 17.0)

I created my own state variables for each of your variables, so the only difference in mine and yours is a) the actual data/view model that your variables are in and b) the .drkBlueContainer

Is the dark blue container custom? Maybe this could be causing an issue.

Can you try running this code snippet on that iPad and let me know if it crashes?


import SwiftUI

struct ContentView: View {
    @State private var userID = 0
    @State private var text: String = ""    
    @State private var text2: String = ""
    @State private var availableRoles: [String] = ["1", "2"]
    @State private var selectedRole: String = "1"
    @State private var createdAt: Date = Date.now
    @State private var toggle: Bool = false
    
    var body: some View {
        HStack(alignment: .top){
                        
                            VStack{
                                if(userID != -1){
                                    HStack{
                                        Text("Employee-No.:")
                                        Spacer()
                                        TextField("Employee-No.", value: $userID, format: .number).textFieldStyle(.roundedBorder).disabled(true).containerRelativeFrame(.horizontal, count: 10, span: 3, spacing: 0)
                                    }
                                }
                                HStack{
                                    Text("Family Name:")
                                    Spacer()
                                    TextField("Family Name", text: $text).textFieldStyle(.roundedBorder).containerRelativeFrame(.horizontal, count: 10, span: 3, spacing: 0)
                                }
                                HStack{
                                    Text("Given Name:")
                                    Spacer()
                                    TextField("Given Name", text: $text2).textFieldStyle(.roundedBorder).containerRelativeFrame(.horizontal, count: 10, span: 3, spacing: 0)
                                }
                                
                                HStack{
                                    Text("Role:")
                                    Spacer()
                                    Picker("Select role", selection: $selectedRole){
                                        ForEach(availableRoles, id:\.self){ obj in
                                            Text(obj).tag(obj)
                                        }
                                    }.pickerStyle(.menu).containerRelativeFrame(.horizontal, count: 10, span: 3, spacing: 0).background(.blue).cornerRadius(5).foregroundColor(.blue).tint(.blue)
                                     
                                //If I add this spacer, it works. If not, it crashes
                                //Spacer().frame(width: 0.0)
                                }.disabled(userID != -1)
                                
                            }
                        Divider()
                        
                            VStack{
                                HStack{
                                    Text("Created at:")
                                    Spacer()
                                    TextField("Created at", value: $createdAt, format: .dateTime).textFieldStyle(.roundedBorder).disabled(true).containerRelativeFrame(.horizontal, count: 10, span: 3, spacing: 0)
                                }
                                HStack{
                                    Toggle("Terminates:", isOn: $toggle)
                                        .padding(.trailing, 2.0)
                                }
                                if(toggle){
                                    HStack{
                                        DatePicker("Terminates at:", selection: $createdAt, displayedComponents: .date).pickerStyle(.automatic)
                                    }}
                            }
                    }
    }
}

#Preview {
    ContentView()
}

Hi @DTS Engineer,

thank you for your reply and your effort :-)

My App-Target is iOS 17.5, Minimum is 17.4.

I was able to run your code as it should on both test devices (iPad Pro M2 and iPad Air 4th Gen, both iOS 17.5.1). I also added my custom color-assets, no problem here.

Next I replaced my original code in my app with your code. And surprise: It worked as it should.

So, I tried to replace your code by my original code line by line. This worked fine for the first few lines, until I reached the line with

Text("Created at:")

What needs to be mentioned here, is that the original string for this label is German and a bit longer ("Mitarbeiter erstellt am:"). So, as soon as I change the string of the English version to the longer, German version the app crashes on my iPad Air 4th Gen, while it is still working absolutely fine on my (larger) iPad Pro. Maybe the word "crash" is not specified enough here: Before the app crashes, CPU is running at 99% the whole time and the memory size needed for my app is currently increasing up to the point were the device is running out of available memory.

As soon as I remove three characters from the string (e.g. "Mitarbeiter erstellt:", removing (space)am), it is working fine again.

I can even replace the second word (erstellt) with another word like "angelegt", which also has eight characters and it's working fine again.

However, it is quite frustrating, as I can not reproduce the error in the clean app with your code. I even rebuild my layout in the clean test app and all is working fine as it should.

I also tried to debug my app, as my first thought was that I am running an infinite loop somewhere. However, this does not seem to be the case.

So, workaround found, but the source for this crash is still open...

SwiftUI Crashes only on A-Chip device
 
 
Q