0

I am trying to create a "simulated" interaction with a server by showing an activity indicator when the user clicks the Save button. (the finished app will actually interact with the server) I set up an IBAction for the save button and I am calling the activityIndicator, animating it and then I have a pause. Finally, the activity indicator hides. Only problem, the activity indicator is not showing. If I comment out the NSThread.sleepForTimeInterval(4) and the activityIndicatory.stopAnimating, the activity indicatory shows. I have tried to move them out of the IBAction for the Save button but that caused the code to error. Here is the code:

@IBAction func saveDTrans(sender: UIBarButtonItem) {

    activityIndicator.hidden = false
    activityIndicator.startAnimating()

    //pause code to let the activityIndicator show for a bit
    NSThread.sleepForTimeInterval(4)

    activityIndicator.stopAnimating()
    activityIndicator.hidden = true
}
Greg
  • 427
  • 3
  • 7
  • 21
  • take a look at this one http://stackoverflow.com/a/28893660/2303865 – Leo Dabus May 30 '15 at 01:24
  • I see a lot of code for building an activity indicator, and I am have that in my app in other places. For this issue, I simply put an activityIndicator in the view with an IBOutlet. I will use some of the code from your example though to add a label to my activityIndicator. thanks for that. – Greg May 30 '15 at 01:40
  • Unfortunately, I still have my issue of trying to call my activityIndicator, pause for 4 seconds and then hide the activityIndicator. – Greg May 30 '15 at 01:40

2 Answers2

1

I dont think you want to tell the thread to sleep as this is the main thread and the activity indicator wont run. Its also not good practice.

You are better off putting it in a dispatch_after block

@IBAction func saveDTrans(sender: UIBarButtonItem) 
{
    activityIndicator.hidden = false
    activityIndicator.startAnimating()

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
        activityIndicator.stopAnimating()
        activityIndicator.hidden = true
    }
}
some_id
  • 29,466
  • 62
  • 182
  • 304
  • I continued looking for a solution and had come across another similar question with this code. I copied and pasted your suggestion in but I am getting an multiple errors. "Consecutive statements on a line must be separated by ','", "Missing argument for parameter 2 in call", "Expression resolves to an unused function", "Braced block of statements is an unused closure", "Expected expression". I don't know where to start. – Greg May 30 '15 at 01:24
0

The code below will stop the main thread:

NSThread.sleepForTimeInterval(4)

Try this instead:

@IBAction func saveDTrans(sender: UIBarButtonItem) {

    activityIndicator.hidden = false
    activityIndicator.startAnimating()

    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC)))
    dispatch_after(delayTime, dispatch_get_main_queue()) {
        activityIndicator.stopAnimating()
        activityIndicator.hidden = true
}
Icaro
  • 14,585
  • 6
  • 60
  • 75