Search code examples
iosswiftswift2today-extension

Widget data doesn't update when scrolling - Swift


I have a widget with data. When I launch today extension my widget data is updating and showing in real time. But when I scroll the notification center and return to my widget, it doesn't update. I tried several different methods but they didn't help me. I wrote below the last method which I tried.

     override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "updateLabels", userInfo: nil, repeats: true)
    }


    func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
        // Perform any setup necessary in order to update the view.

        // If an error is encountered, use NCUpdateResult.Failed
        // If there's no update required, use NCUpdateResult.NoData
        // If there's an update, use NCUpdateResult.NewData
        completionHandler(.NewData)
    }


   func updateLabels() {
        runtimeLabel.text = returnTime() + "     " + returnDay()
    }

    func returnTimeInterval() -> NSTimeInterval {
        let uptime = NSProcessInfo().systemUptime
        return uptime
    }

    func returnTime() -> String {
        dateFormatter.unitsStyle = .Short
        dateFormatter.allowedUnits = [.Day, .Hour, .Minute, .Second]
        dateFormatter.zeroFormattingBehavior = .Pad
        let time = dateFormatter.stringFromTimeInterval(returnTimeInterval())!
        return time
    }

    func returnDay() -> String {
        dateFormatter.unitsStyle = .Short
        dateFormatter.allowedUnits = [.Year, .Month, .Day]
        dateFormatter.zeroFormattingBehavior = .Pad
        let date = NSDate(timeInterval: -returnTimeInterval(), sinceDate: NSDate())
        let formatter = NSDateFormatter()
        formatter.locale = NSLocale.currentLocale()
        formatter.dateStyle = .MediumStyle
        let megaDate = formatter.stringFromDate(date)
        return megaDate
    }

Solution

  • I tried the same and it worked with the code below:

    @IBOutlet weak var infoLabel: UILabel!
    var timer = NSTimer()
    var counter = 0
    
        override func viewWillAppear(animated: Bool) {
                super.viewWillAppear(animated)
    
                timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateLabel"), userInfo: nil, repeats: true)
            }
    
        func updateLabel(){
                counter += 1
                myLabel.text = "Test \(counter)"
            }
    

    Update:

    viewWillAppear and viewDidAppear should be called after when the widget is active and viewDidDisappear should be called when scrolling (leaving the widget). As for now the viewDidDisappear is working as expected but not viewWillAppear and viewDidAppear on scrolling.

    It´s a known bug that this is not working properly, you can read more information in this post at Apples forum and check the bug status report here.