Swift Xcode6 UIActivityIndicatorView Slow to display

404 views Asked by At

How do I get the UIActivityIndicatorView to display first, then execute other code?

I've experimented with using sleep, and it works but it doesn't "feel" right and adds an extra second to processing a bunch of core data stuff. I've also tried dispatching it to the main thread which only works some of the time. (I'm guessing when the rest of the block is executed outside of the main thread).

Ideally as soon as a user touches the button the instance of the UIActivityIndicatorView would display (which seems to happen where I've used it in other apps by itself or with other minimal processing).

Details: I have an IBAction connected to a button that executes a bunch of core data stuff, sometimes including images, that takes between 1 - 3 seconds to finish. When it finishes it dismisses the view controller. The view controller where this is executed is presented as a modal over current context.

1

There are 1 answers

0
zisoft On BEST ANSWER

Here is a code snippet:

// get the background queue
let bg_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

dispatch_async(bg_queue, {
    // long running code here...

    dispatch_async(dispatch_get_main_queue(), {
        self.activityIndicator.stopAnimating()
    })
})