Matt Spoon
Matt Spoon

Reputation: 2830

background processing a difficult task on viewDidLoad in swift

I am currently making an app which initiates a very long task on the viewDidLoad method. The nature of it is stopping the view from loading at all for extended periods of time and sometimes causes the app to crash. As such, I need to be able to process this particular task in the background so that the view can load instantly and then, in the background, complete the task and update the view when it is done. Does anybody know how to do this?

Upvotes: 1

Views: 521

Answers (1)

Ramis
Ramis

Reputation: 16609

Try to use GCD similar to this:

let backgroundQueue = dispatch_get_global_queue(CLong(DISPATCH_QUEUE_PRIORITY_HIGH), 0)
dispatch_async(backgroundQueue) {
    // Do your stuff on global queue.

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        // After finish update UI on main queue.
    })
}

Or you can use NSOperationQueue. At WWDC 2105 was very nice talk about it. https://developer.apple.com/videos/wwdc/2015/?id=226

Upvotes: 1

Related Questions