How to add a spinner?

how to add a spinner, please tell me!

Well, they say to add an “indeterminate” indicator… which just indicates that a process is busy by animating itself and then completed by stopping… versus a determinate indicator which would give you visual feedback of the actual amount of progress (i.e., a progress bar that shows that 30% of the page has completed loading). So, I added the “Indeterminate Circular Progress Indicator” to the upper right corner of my MainWindowController.xib window. I then added an outlet for this indicator, called progressIndicator, and just added a startAnimation call within MainWindowController.windowDidLoad() and a stopAnimation within the closure given to ScheduleFetcher.fetchCoursesUsingCompletionHandler.

Here’s my code…[code]class MainWindowController: NSWindowController {

override var windowNibName: String! {
    return "MainWindowController"
}

@IBOutlet weak var tableView: NSTableView!
@IBOutlet weak var arrayController: NSArrayController!
@IBOutlet weak var progressIndicator: NSProgressIndicator!

let fetcher = ScheduleFetcher()
dynamic var courses: [Course] = []

override func windowDidLoad() {
    super.windowDidLoad()
    
    tableView.target = self
    tableView.doubleAction = Selector("openClass:")
    
    progressIndicator.startAnimation(self)
    progressIndicator.hidden = false
    
    fetcher.fetchCoursesUsingCompletionHandler({ (result) in
        self.progressIndicator.stopAnimation(self)
        self.progressIndicator.hidden = true
        switch result {
        case .Success(let courses):
            print("Got courses: \(courses)")
            self.courses = courses
        case .Failure(let error):
            print("Got error: \(error)")
            NSAlert(error: error).runModal()
            self.courses = []
        }
        })
}

func openClass(sender: AnyObject!) {
    if let course = arrayController.selectedObjects.first as? Course {
        NSWorkspace.sharedWorkspace().openURL(course.url)
    }
}

}[/code]
I added the calls to set the hidden property of the indicator also, because I didn’t want to see it after the course fetch completed. The whole thing was very brief on my computer, but I do see it spin briefly then disappear. What would be better, I think, would be to change the mouse cursor to an indeterminate spinner while hovered over our application window. But that would likely require the use of mouse tracking via mouseEntered and mouseExited as well as some code to change the mouse pointer to the “beachball” animated spinner. I just tried to follow the book’s suggestion.

For something bigger, where you had a lengthy web fetches… I would think you would want to add a Determinate Bar Progress Indicator. However, what I think you would have to do is create a repeating NSTimer that would fire every .25 seconds, or whatever, and then update the indicator with the actual progress. The NSURLSessionTask has both a “countOfBytesExpectedToReceive” and a “countOfBytesReceived” property. BUT to use them, I’m guessing you would have to perform the NSURL fetching with a different NSURLSession method because we are using the completion handler one which just reports back to our handler when done. Or maybe we could save off the “task” within ScheduleFetcher.fetchCoursesUsingCompletionHandler to a class property or global variable that we could then ask for those “countOfBytes” properties to update a determinate indicator on an NSTimer triggered basis. I didn’t pursue it because the course fetching was so quick, and I moved on.

achamp, you are a good person. :smiley: Thank you so much!