How to update default widget every 5 seconds

I am trying to update the contents of my today's widget extension every x seconds since I am trying to implement something like a diaspora. Therefore, I saved all the necessary data using common default values. Loading data from storage is fine, but completing the Handler extension:

func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)!) { //I do load the content here completionHandler(NCUpdateResult.NewData) } 

It is called only once. How can I implement a function that says "newData" is available every x seconds?

+5
source share
2 answers

One way is NSTimer. This is useful for calling a method every x seconds.

 var timer : NSTimer? override func viewDidLoad() { super.viewDidLoad() timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "animateFrame:", userInfo: nil, repeats: true) } func animateFrame(timer: NSTimer) { // Do something } 

In this case, you can call animateFrame: every 3 seconds.

+2
source

There are many ways to do this. One way is to add an observer.

Like this

 NSNotificationCenter.defaultCenter().addObserver(self, selector:"updateStuff", name: UIApplicationWillEnterForegroundNotification, object: nil) func updateStuff() -> Void{ // Update your Stuff... } 

Thus, Selection calls a function in your Today Widget class.

Thus, your widget will call your function when your wider one introduces the foreground.

Please note that your widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) need to download content from the Internet. What you do not need an observer

Hope I help you.

+1
source

All Articles