Updating the UI Using Dispatch_Async in Swift -


In my code I am simple to loop, which prepares loops for loops to loop 100 times with nested After the delay, I am updating the progress visual element in the UI via a dispatch_assink. However, I can not get the UI to update. Does anyone know that UI is not updated? Note: The print statement below is used to verify that the loop is being properly looping.

  i in 0 ..  

three comments, two basic, a little more advanced:

  1. Your loop Will not be able to update the UI in that main thread unless the loop itself is not running on another thread. Therefore, you can send it in some background queues. In Swift 3:

      Dispatch Quayu Global (QoS: Disability) for .async {i in 0 .. .. & lt; } : kNumberOfIterations {} // Here consuming DispatchQueue.main.async {sometime // Now the main thread self.progressView.setProgress (really float (i) / float (kNumberOfIterations), animated) UI update} 

    In Swift 2:

      sending_security (sender_gate_global_que (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {ii in 0 .. .. kNumberOfIterations {// some time dispatch_async consuming here (dispatch_get_main_queue ()) {// Now the main thread self.progressView.setProgress (float (i) / float (kNumberOfIterations), Animated: True) UI update}}} < Note also that progress is a number from 0.0 to 1.0, so you probably want to split the number of maximum iterations for the loop.   
  2. If the UI update comes faster than the background thread then the UI can handle them, the main thread can get backgong with the update requests (this is actually Really very slow). To resolve this, using the sender source to reduce the "Update UI" function with the actual background updating process can be considered.

    A You can use DispatchSourceUserDataAdd , Post (Swift 2, it is a dispatch_source_t DISPATCH_SOURCE_TYPE_DATA_ADD ) Adding from the background thread to the call ( sending_source_reference_data in SWIFT 2) is as often desired, and the UI will process them as soon as possible, but when it calls, Will be called together data ( dispatch_source_get_data in SWIFT 2) if the background Updates come faster than the UI, otherwise you can process them. It achieves maximum background performance with optimum UI updates, but more importantly, it ensures that the UI will not create an obstacle.

    Then, to declare the variable to keep track of some progress: Var Progress Countter: UInt = 0

  3. and Now your loop can be a source, it can define what to do when the source is updated, and then launch the asynchronous loop which updates the source. Swift 3 has this:

      progressCounter = 0 // create the sending source which will handle the event on the main line source = dispatchsource.MACUSAdAdSource (Qatar: .man) // what to do when source programs organized source.setEventHandler () take {[Unowned own] self.progressCounter + = source.data in self.progressView.setProgress (float (self.progressCounter) / float (kNumberOfIterations), animated: True)} // start source source.resume () // now for background in DispatchQueue.global (qos: .atility) .async {i for 0 Switch to & lt; KNumberOfItations {// //> Take some time to // // now the remittance source source update. Add (Data: 1)}}  

    In Swift 2:

      ProgressCounter = 0 // create a rematch source that will handle the event on the main line Source = sender_source_cent (DISPATCH_SOURCE_TYPE_DATA_ADD, 0, 0, dispatch_get_main_queue ()); // to do / float when the event source programs dispatch_source_set_event_handler (source) {[Unowned own] + = dispatch_source_get_data (Source self.progressCounter) self.progressView.setProgress (float (self.progressCounter) (kNumberOfIterations it do), animated: True)} // Start source Preshn_sndrb (source) // now background Dispac_asink (Dispac_get_globl_kyuu (Dispac_ QUEUE_PRIORITY_DEFAULT; 0)) at 0 to begin the loop (i & lt; KNumberOfIterations {/ / here to take some time Is / now remitting The source dispatch_source_merge_data (source, 1) update;}}  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -