My app has calls to a database in the .onAppear method of a view, but I want to use the preview feature of SwiftUI. Is there a way I can mock or disable this function in the preview window?
struct MyView: View {
@State var dataArray: [Data] = [Data]()
var body: some View {
VStack {
ForEach(dataArray) { data in
Text(data.text)
}
.onAppear {
getData()
}
}
private func getData() {
someCallToDatabase() { returnedData in
self.dataArray = returnedData
}
}
struct MyView_Previews: PreviewProvider {
static var previews: some View {
MyView()
}
}
Basically I'd like to either pass the data in manually without it being overwritten, or somehow add an extension to my database caller class where it automatically sets some dummy data when it knows it's just for the preview.
Thanks a bunches, crunches!