Reputation: 43
I am writing an iOS application in Swift 2 for iOS 9, where I have one method that I would like to run in the background right after login while the user navigates between 3 different view controllers.
My app is structured like so:
After logging in, the user segues into 1 of 3 different view controllers. However, I would like to pull data in the background from the network and then update Core Data, regardless of the current view controller. Is this possible? Thanks!
Upvotes: 0
Views: 815
Reputation: 2482
Just write a method in another Class.
class Utils
{
func doSomeJob()
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
//All stuff here
})
}
}
This code will be executed in a background thread. And then in each ViewController just call this Method
Utils().doSomeJob()
Upvotes: 1
Reputation: 342
Yes it is possible,First create a singleton class called BackgroundTaskController that you can call from any view controller. If you want to run background tasks, when your app is in the background then you need to enable run in the background capability in the info.plist file.
Upvotes: 1