如何每隔x秒更新今天的小部件

时间:2020-12-08 01:16:05

I try to update the content of my today widget extension every x seconds since I try to realize something like a diashow. Therefor I have stored all required data using shared defaults. Loading data from the storage works perfect but the completionHandler of the extension:

自从我尝试实现类似diashow之类的东西以来,我每隔x秒尝试更新我今天的小部件扩展的内容。因此,我使用共享默认值存储了所有必需的数据。从存储加载数据工作完美,但扩展的completionHandler:

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

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

只被叫一次。如何实现一个每x秒可以使用“newData”的函数?

2 个解决方案

#1


2  

The one way is NSTimer. It is useful for calling method every x seconds.

一种方式是NSTimer。每隔x秒调用方法很有用。

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 the case, you can call animateFrame: every 3 seconds.

在这种情况下,您可以调用animateFrame:每3秒钟。

#2


1  

There is a lot of way 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...
}

So the Selection calls an function in your Today Widget class.

因此,Selection会在Today Widget类中调用一个函数。

So your Widget will call your function when your wider will enter foreground.

所以当你的更广泛的人进入前景时,你的Widget会调用你的函数。

Note that your widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) Is there to load content from the Web. Than you don't need an observer

请注意,您的widgetPerformUpdateWithCompletionHandler(completionHandler:((NCUpdateResult) - > Void))是否可以从Web加载内容。比你不需要观察者

I hope I could help you

我希望我能帮助你

#1


2  

The one way is NSTimer. It is useful for calling method every x seconds.

一种方式是NSTimer。每隔x秒调用方法很有用。

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 the case, you can call animateFrame: every 3 seconds.

在这种情况下,您可以调用animateFrame:每3秒钟。

#2


1  

There is a lot of way 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...
}

So the Selection calls an function in your Today Widget class.

因此,Selection会在Today Widget类中调用一个函数。

So your Widget will call your function when your wider will enter foreground.

所以当你的更广泛的人进入前景时,你的Widget会调用你的函数。

Note that your widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) Is there to load content from the Web. Than you don't need an observer

请注意,您的widgetPerformUpdateWithCompletionHandler(completionHandler:((NCUpdateResult) - > Void))是否可以从Web加载内容。比你不需要观察者

I hope I could help you

我希望我能帮助你