It seems to work on the fact of refreshing the ProgressView but I get warnings that tell me I’m doing it the wrong way:
UI must be changed from the main thread.
When you change values with @Published , the UI is updated.
You can use DispatchQueue.main.async to change values from main thread.
Like this:
DispatchQueue.main.async {
p.progress += 10.0
}
Also, what I did does not update correctly the final values.
I suggest below ⬇️
Add @Published var induction = 0.0 to Params class
Change value of induction (from main thread)
Show on UI
Like this:
struct ViewMAG_MultiStep: View{
@FocusState var isFocused: Bool
@EnvironmentObject var p: Params
@State private var showResults = false
// @State private var induction = 0.0
var body: some View{
List{
Button("Compute") {
calcMultiStep(p: p)
showResults.toggle()
}
.sheet(isPresented: $showResults) {
// Text("\(induction)")
Text("\(p.induction)")
ProgressView("Progress", value: p.progress, total: 100)
}
}
.navigationTitle("Multi-step")
}
}
class Params: ObservableObject {
@Published var progress = 0.0
@Published var value = 0.0
@Published var induction = 0.0
}
func calcMultiStep(p: Params) {
var induction = 0.0
DispatchQueue.global().async {
for i in 0...5 {
induction += Double(i) * calcSingleStep(p: p)
// from main thread
DispatchQueue.main.async {
p.progress += 10.0
}
}
print("Final value of induction: \(induction)")
// from main thread
DispatchQueue.main.async {
p.induction = induction
}
}
}
I am not a native English speaker, so I apologize if my English is incorrect.
Topic:
Programming Languages
SubTopic:
Swift
Tags: