0

I have following code and trying to simulate a heavy task. Where do I have to activate and deactivate the progressView? In this Video it doesn't look right (the cirle should as long rotate as the number is about to change, when it’s changing the circle should disappear): Video_imgur

struct ContentViewDispatch: View{
    @State var button_tap_count = 0
    @State var dispatch_start = 0
    @State var i = 0
    @State var showProgress = false
    var body: some View{
        VStack{
            Text("Tapped: \(button_tap_count)")
            Text("Dispatch: \(dispatch_start)")
            Text("number: \(i)")
            Button("reset"){
                i = 0
            }
            ProgressView().progressViewStyle(CircularProgressViewStyle()).opacity(showProgress ? 1 : 0)
            
            Button("count"){
                button_tap_count += 1
                var k = i
                DispatchQueue.global(qos: .userInitiated).async {
                    DispatchQueue.main.async {
                        showProgress = true                    <----- activate
                        dispatch_start += 1
                    }
                    for _ in 0..<5_000_000{
                        k += 1
                    }
                    DispatchQueue.main.async {
                        showProgress = false                   <------ deactivate
                        i = k
                    }
                }
            }
        }
    }
}
sheldor
  • 115
  • 12
  • What does *right* and *not right* mean? – vadian Dec 16 '22 at 14:14
  • "the cirle should as long rotate as the number is about to change, when it’s changing the circle should disappear": can you please re-phrase it? I don't understand what is wrong and what would be the right behavior. – HunterLion Dec 16 '22 at 15:27
  • @HunterLion when the number changes from 10000000 to 15000000 the circle disappears a bit after the number changed. The circle should stop rotating in the moment the number jumps from one to another value. And at the end the circle stops rotating but one moment later the value changes to 50000000. Here the circle should disappear right after this change – sheldor Dec 16 '22 at 15:41
  • works just fine with me, does exactly what you describe (Xcode 14.1, iOS simulator 16.1) – ChrisR Dec 16 '22 at 17:35
  • https://stackoverflow.com/questions/73538764/trouble-running-async-functions-in-background-threads-concurrency/73542522#73542522 – lorem ipsum Dec 17 '22 at 05:25

0 Answers0