6

The today widget is drawn correctly when it is added to the today view. But if you user comes back to it later, the viewDidLoad function is not called and it is showing stale data. Should viewDidLoad be called everytime? Is there an iOS 9 / Xcode 7 beta 6 bug?

Edit: Added that widgetPerformUpdateWithCompletionHandler not called either. I have breakpoints set and print functions

func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
    print("in widgetPerformUpdateWithCompletionHandler")
    fetch()

    completionHandler(NCUpdateResult.NewData)
}
Jason Hocker
  • 6,879
  • 9
  • 46
  • 79
  • I have never worked with widgets but normally `viewDidAppear` is called every time screen is presented, `viewDidLoad` is only called when view initially loaded. – SpaceDust__ Aug 31 '15 at 20:14
  • Are you not using `widgetPerformUpdateWithCompletionHandler:` see [here](https://developer.apple.com/library/ios/documentation/NotificationCenter/Reference/NCWidgetProviding_Protocol/index.html#//apple_ref/occ/intfm/NCWidgetProviding/widgetPerformUpdateWithCompletionHandler:) – soulshined Aug 31 '15 at 20:15
  • 3
    I've the same issue. `widgetPerformUpdateWithCompletionHandler` used to be called each time the widget was displayed. iOS9 calls this methods far less. But I didn't find any documentation about this change. – just.do.it Sep 21 '15 at 15:59

2 Answers2

5

When you scroll a widget off and back on screen, the same controller instance will be reused for a short amount of time (appears to be ~30 seconds in my testing), and viewDidLoad and widgetPerformUpdateWithCompletionHandler: will not be called.

However, viewWillAppear and viewDidAppear will be called every time your widget is displayed.

Christopher Pickslay
  • 17,523
  • 6
  • 79
  • 92
0

Posting my own answer, but would like discussion on this code - should it be there or how to properly do it?. We had in this method, and by removing it the widget began to work correctly

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{

    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

    if let safeCoordinator = coordinator as UIViewControllerTransitionCoordinator?
    {
        print("coordinator != nil")
        safeCoordinator.animateAlongsideTransition({ context in
            self.tableView.frame = CGRectMake(0, 0, size.width, size.height)
            }, completion: nil)

    }
    else
    {
        print("coordinator == nil")
    }
}
Jason Hocker
  • 6,879
  • 9
  • 46
  • 79
  • This code looks like you're trying to resize your table view in response to something else changing the height of your view. What is that something else? If you're using auto layout, your table view should resize to fit the number of cells it displays. Otherwise you can set [preferredContentSize](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instp/UIViewController/preferredContentSize) to specify exactly the height you want. – Christopher Pickslay Sep 01 '15 at 15:00
  • @ChristopherPickslay Doesn't Apple discourage the use of preferredContentSize ? – Supertecnoboff Apr 27 '16 at 09:20
  • 1
    @Supertecnoboff not that I'm aware of: `If you don’t use Auto Layout, you can use the UIViewController property preferredContentSize to request a height for the widget.` https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/Today.html#//apple_ref/doc/uid/TP40014214-CH11-SW5 – Christopher Pickslay Apr 28 '16 at 04:16