1

I am showing an alertController and if the user clicks Yes an ProgressView should be shown, but unfortunately the Progressview and the label does not appear. How can I refresh my ViewController. Here the cod that is executed for the yes-handler of the alertController. The code will be executed without problem, but the progressview is not appearing:

func initProgressView(){
    turn = 0
    let xCoord = self.view.center.x
    let yCoord = self.view.center.y + 10
    progressLabel = UILabel(frame: CGRect(x: xCoord, y: yCoord, width: 100, height: 23))
    progressLabel.text = "0 %"



    progressLabel.font = UIFont.boldSystemFontOfSize(14)
    progressView.center = self.view.center
    progressView.trackTintColor = UIColor.lightGrayColor()
    progressView.tintColor = UIColor.blueColor()
   // self.view.backgroundColor = UIColor.yellowColor()
    self.view.addSubview(progressLabel)
    self.view.addSubview(progressView)

}

here the complete sequence of call: initProgressView() //see previous post

then call of importData:

func importData (source : ImportDataInterface, data : NSData, progressStep : Int) {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
            source.importData(data)
            dispatch_async(dispatch_get_main_queue(), {
                self.counter += progressStep
                return
            })
        })



}

and finally in the calling function: progressView.removeFromSuperview() progressLabel.removeFromSuperview()

How can I refresh the ViewController or what else could be the reason why the progressView does not appear. Can it be, that constraints or autolayout issues are the problem.

thanks Arnold

Arnold Schmid
  • 163
  • 12

2 Answers2

0

looks like that progressView overlaps progressLabel, probably you need change last 2 strings order?

self.view.addSubview(progressView)
self.view.addSubview(progressLabel)
Igor
  • 1,537
  • 10
  • 12
0

You haven't provided enough information. You need to show us the sequence of calls.

iOS doesn't render changes to the screen until your code returns. The following coding pattern will not work:

create progress indicator
add progress indicator to superview
start progress indicator spinning
do long-running task
stop progress indicator

Instead, what you have to do is something like this:

create progress indicator
add progress indicator to superview
start progress indicator spinning
invoke long-running task with a call to dispatch_after and a delay of 0:
dispatch_after(main queue, delay 0)
{
   do long running task
   stop activity indicator
}

The call to dispatch_after queues up your closure to run on the main thread the next time your code returns and the event loop even if the delay value is 0.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • here the sequence of calls: – Arnold Schmid Dec 31 '15 at 13:22
  • all my code is executed in the yes-handler of the alert. How can I return to the main ViewController and then execute the code. So perhaps there lies the problem. I mean I want to read data from a file when the user taps on yes. the calls are: 1) initprogressview 2) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), 3) read portion of data 4) dispatch_async(dispatch_get_main_queue(), 5) update progressview. thanks arnold – Arnold Schmid Dec 31 '15 at 13:44
  • Now I localized the problems. It lies in the fact that I am calling all the methods in the yes handler. It shows me the progressview when I only call the method initProgressView(). But how can I show the progressview, then returning to the calling ViewController and execute the rest of the code. thanks arnold – Arnold Schmid Dec 31 '15 at 14:38
  • Thanks Duncan, that was it. – Arnold Schmid Jan 02 '16 at 17:33
  • What was it? Explain, for the benefit of others reading this thread. – Duncan C Jan 03 '16 at 00:41
  • The problem was that I had to call dispatch_after. It seems that this is the only way to update the screen returning from an other controller. In the callback I integrated the dispatch_after call and in the closure I executed the background task. – Arnold Schmid Jan 03 '16 at 14:21