I want to set a @State Variable if a View appears by calling a Service.
Calling the Service in onAppear crashes the app.
Calling the Service in a buttons action works fine.
struct ContentView: View {
@State var dirContent : [String] = []
var body: some View {
VStack {
List {
ForEach(dirContent, id: \.self){
fileName in
Text(fileName)
}
}
Button("Get Data"){self.dirContent = Service.getData()}
}
.onAppear{
//self.dirContent = Service.getData2() // Crashes!!
}
}
}
class Service{
static func getData()->[String]{
// ... get Data from somewhere
return ["line1", "line2"]
}
static func getData2()->[String]{
// ... get Data from somewhere
return ["line4", "line5"]
}
}
What's going wrong?