0

I use Xcode 10 and Swift 4.2

in viewDidload () of a UIViewController, I declare an observer:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(OtherFunctionsViewController.dataImportExportFunc), name: .ImportExportdata, object: nil)
} 

From the network, I receive a message (based on .ImportExportdata name) that increases a float value called ProgressLevel from 0.0 to 1.0

I have declared this value to be followed by IOS, and it works. It prints correctly the value that will be updated

@IBOutlet weak var OtherFuncProgressView: UIProgressView!
var ProgressLevel : Float = 0.0 {
    willSet // "name" is ABOUT TO CHANGE
    {
        print("ProgressLevel WILL be set...")
        print("from current value: \(ProgressLevel)")
        print("to new value: \(newValue).\n")
        DispatchQueue.global(qos: DispatchQoS.QoSClass.userInteractive).async
            {
                DispatchQueue.main.async
                    {
                        print("dispatch queue main")
                        self.OtherFuncProgressView.setProgress(Float(self.ProgressLevel), animated: true)
                        self.OtherFuncProgressView.setNeedsDisplay()
                }
        }
    }
}

ProgressLevel is correctly printed, but the progressView is never updated on screen.

What is wrong?

Thanks for your answer

I change my code according to your proposal as follows:

    var ProgressLevel : Float = 0.0

    {
        willSet // "name" is ABOUT TO CHANGE
        {
            print("ProgressLevel WILL be set...")
            print("from current value: \(ProgressLevel)")
            print("to new value: \(newValue).\n")
//            DispatchQueue.global(qos: DispatchQoS.QoSClass.userInteractive).async
//                {
                    DispatchQueue.main.async
                        {
                            print("dispatch queue main value \(newValue)")
//                            self.OtherFuncProgressView.setProgress(Float(self.ProgressLevel), animated: true)
                            self.OtherFuncProgressView.setProgress(Float(newValue), animated: true)

                            self.OtherFuncProgressView.setNeedsDisplay()
                    }
//            }
        }
}

I got: ProcessImportFile: file:///Users/guydesbief/Library/Developer/CoreSimulator/Devices/17DA59AA-430F-4395-B2F3-FF2A81EEB429/data/Containers/Data/Application/91B3BA10-45C1-4090-A401-5CDA03EED8C8/tmp/2019-2-21_2018-2-19_Mois.csv 2019-02-21 11:18:22.755903+0100 SolarPanelSurveyIos[85928:4363118] razImport Done ProgressLevel WILL be set... from current value: 0.0 to new value: 0.0.

2019-02-21 11:18:22.758389+0100 SolarPanelSurveyIos[85928:4363118] Statistic WindowController file Stream OK 2019-02-21 11:18:22.760550+0100 SolarPanelSurveyIos[85928:4363118] no buffer! dispatch queue main value 0.0 2019-02-21 11:18:22.760800+0100 SolarPanelSurveyIos[85928:4363118] Statistic WindowController No more data read from file! 2019-02-21 11:18:22.761242+0100 SolarPanelSurveyIos[85928:4363118] Field Header: Import Record Ended with char Nbfields = 4 , Current record: 0 2019-02-21 11:18:22.761342+0100 SolarPanelSurveyIos[85928:4363118] Import ended 40 records, 38 Maxrecords

ProgressLevel WILL be set... from current value: 0.0 to new value: 0.05263158.

ProgressLevel WILL be set... from current value: 0.05263158 to new value: 0.13157895.

ProgressLevel WILL be set... from current value: 0.13157895 to new value: 0.21052632.

ProgressLevel WILL be set... from current value: 0.21052632 to new value: 0.28947368.

ProgressLevel WILL be set... from current value: 0.28947368 to new value: 0.36842105.

ProgressLevel WILL be set... from current value: 0.36842105 to new value: 0.4473684.

ProgressLevel WILL be set... from current value: 0.4473684 to new value: 0.5263158.

ProgressLevel WILL be set... from current value: 0.5263158 to new value: 0.6052632.

ProgressLevel WILL be set... from current value: 0.6052632 to new value: 0.68421054.

ProgressLevel WILL be set... from current value: 0.68421054 to new value: 0.7631579.

ProgressLevel WILL be set... from current value: 0.7631579 to new value: 0.84210527.

ProgressLevel WILL be set... from current value: 0.84210527 to new value: 0.92105263.

ProgressLevel WILL be set... from current value: 0.92105263 to new value: 1.0.

2019-02-21 11:18:23.631275+0100 SolarPanelSurveyIos[85928:4363118] Temps d'exécution: 871867

dataImportExportFunc No more datas number of records : 0 / 39.000000 execTime: 0.8719

dispatch queue main value 0.05263158

dispatch queue main value 0.13157895

dispatch queue main value 0.21052632

dispatch queue main value 0.28947368

dispatch queue main value 0.36842105

dispatch queue main value 0.4473684

dispatch queue main value 0.5263158

dispatch queue main value 0.6052632

dispatch queue main value 0.68421054

dispatch queue main value 0.7631579

dispatch queue main value 0.84210527

dispatch queue main value 0.92105263

dispatch queue main value 1.0

Dispatch queue is emptied only at the end of process, and I would like to empty it in real time Any suggestions are welcome

La Ciste
  • 11
  • 3
  • 2
    Try just using the `DispatchQueue.main.async ` instead of nesting it inside the `DispatchQueue.global(qos: DispatchQoS.QoSClass.userInteractive).async ` – Shamas S Feb 20 '19 at 02:38
  • what do you need this `self.OtherFuncProgressView.setNeedsDisplay()` line for, anyway? – holex Feb 21 '19 at 12:02

1 Answers1

0

willSet is called before the value is updated, so you are better off using newValue inside the closure:

var progressLevel: Float = 0.0 {
    // "name" is ABOUT TO CHANGE
    willSet {
        print("ProgressLevel WILL be set...")
        print("from current value: \(progressLevel)")
        print("to new value: \(newValue).\n")
        DispatchQueue.main.async {
            print("dispatch queue main")
            self.OtherFuncProgressView.setProgress(Float(newValue, animated: true)
            self.OtherFuncProgressView.setNeedsDisplay()
        }
    }
}
pckill
  • 3,709
  • 36
  • 48