0

I'm making a download App and to keep the user inform about the progress I use an UIView on top of the main View. I've got trouble with updating my labels and progressBars. I try different way to do it like:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
    // do some task
    dispatch_async(dispatch_get_main_queue()) {
        // update some UI
    }
}

And also:

self.lblSpeedDownload.setNeedsDisplay()

But it does not update the UI every time it's call

So I tried this https://stackoverflow.com/a/26744884/2410264 For now it's look like it is the best solution I made but it's still not enough quickly update: it's look like the more I call update function, the more it's take time to update UI (1-2-10-20 seconds...)

Here is my code:

A class variable:

let source = dispatch_source_create(DISPATCH_SOURCE_TYPE_DATA_ADD, 0, 0, dispatch_get_main_queue());

Call when I start download:

dispatch_source_set_event_handler(source) {
        [unowned self] in

        self.updateDownloadView(self.mediaIndex, numberOfMedia: self.numberOfMedia)
    }
dispatch_resume(source)

Call when I want to update UI:

dispatch_source_merge_data(source, 1);

And the update function:

func updateDownloadView(mediaIndex : Float, numberOfMedia:Int32){
    if downloadPopup != nil {
        var tempText:String
        if self.selectedDirectories.count == 1 {
            tempText = "Folder "
        } else {
            tempText = "Folders "
        }
        self.lblFolders.text = tempText + (self.downloadIndex+1).description + "/" + self.selectedDirectories.count.description + " :"

        if numberOfMedia == 1 {
            tempText = "Image "
        } else {
            tempText = "Images "
        }
        self.lblImages.text = tempText + Int(floor(mediaIndex)+1).description + "/" + numberOfMedia.description + " :"

        let images_progress = mediaIndex/Float(numberOfMedia)
        let folders_progress = Float(self.downloadIndex)+images_progress/Float(self.selectedDirectories.count)

        self.progressFolders.setProgress(folders_progress, animated: true)
        self.progressImages.setProgress(images_progress, animated: true)

        self.lblSpeedDownload.text = Util.getByteString(self.downloadPopup.getDownloadSpeedBySecond())+"/s"
    }
}

Do you find something wrong with my code? Do you have other way to update UI?

Thank you for reading me!

Community
  • 1
  • 1
Teiki
  • 200
  • 2
  • 18
  • Let's killing the obvious case : Are you sure that your downloadPopup is not nil ? – CZ54 May 20 '16 at 14:16
  • Yes it is not, in fact it can't be because the uiview is inside the uiviewcontroller. I use hidden property instead of load it when needed. – Teiki May 20 '16 at 15:27
  • add a breakpoint and check if the view is actually getting updated on the Main Thread. – Camo May 20 '16 at 22:00
  • I did that to, and it is update on the main thread – Teiki May 21 '16 at 01:30

0 Answers0