Reputation: 878
Hello
I'm trying to execute a task in background because I need to while the application is running being pulling data from the server. I write the code in the AppDelegate.swift, this is the code that i wrote:
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
var window: UIWindow?
let manager = AppManager.manager
var timer: NSTimer? = nil
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.timer = NSTimer(timeInterval: 1.0, target: self, selector: "UpdateDeviceData:", userInfo: nil, repeats: true)
return true
}
func UpdateDeviceData(timer:NSTimer) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in
print("do some background task")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
print("update some UI")
})
})
}
}
I put a breakpoint in the line "print" and it never being executed.
Please any idea about what could be the problem?
Note:I'm using Xcode 7 beta and Swift
Upvotes: 0
Views: 456
Reputation: 3842
You need to schedule the timer on a run loop - you're just assigning it to a variable. Try using the class method + scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
-
so
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "UpdateDeviceData:", userInfo: nil, repeats: true)
Upvotes: 2