29

Been looking around for an answer but everything seems vague or unclear.

Anyways, just want to know what the purpose of the function:

widgetPerformUpdateWithCompletionHandler

does in the today widget.

According to Apple:

This method is called to give a widget an opportunity to update its contents and redraw its view prior to an operation such as a snapshot. When the widget is finished updating its contents (and redrawing, if necessary), the widget should call the completion handler block, passing the appropriate NCUpdateResult value.

When does the snapshot ever happen? Whenever I debug the extension, widgetPerformUpdateWithCompletionHandler always gets called after loadView. So what is the purpose of explicitly reloading information in this method when I already load the information in loadView?


According to this website: http://www.karlmonaghan.com/tag/today-widget/

In the TodayViewController, there are two places that need to load data from the network – when the widget is created and when widgetPerformUpdateWithCompletionHandler is called. For the former, I load posts in viewDidLoad, so that they should be ready by the time the widget displays. When iOS thinks the widget will be displayed to the user after it has been first displayed, widgetPerformUpdateWithCompletionHandler is called giving the widget a chance to update the posts displayed.

Same question as above.

2
  • cool just realised karl monaghan actually found this question
    – ktzhang
    Commented Aug 29, 2014 at 15:59
  • From appcoda.com/app-extension-programming-today The system takes a snapshot of the widget and periodically, will try to update it. If it updates successfully, the function calls the system-provided completion block with the NCUpdateResult.NewData enumeration. If the update wasn’t successful, then the existing snapshot is used.
    – onmyway133
    Commented Nov 13, 2014 at 5:19

1 Answer 1

12

A widget is not created every time you view the notification center so loadView won't be called every time it is displayed. The notification center instead calls widgetPerformUpdateWithCompletionHandler when it thinks the widget information needs to be updated. From my own debugging it does look like that when the widget is initially created widgetPerformUpdateWithCompletionHandler is called nearly straight away so you could just do all the loading in there but Apple recommends you start the loading process as early in the life cycle as possible.

If the information your widget displays never changes, then you don't have to do anything in widgetPerformUpdateWithCompletionHandler.

12
  • 2
    should I update information outside of widgetPerformUpdateWithCompletionHandler? and from my debugging it looks like loadView is called every time the widget is displayed... so basically whenever widgetPerformUpdateWithCompletionHandler is called, loadView is also called so there is really no point in updating information in widgetPerformUpdateWithCompletionHandler then.
    – ktzhang
    Commented Aug 13, 2014 at 16:57
  • 15
    From what I can tell, not only is loadView called every time the widget is displayed, the widget is fully instantiated every time it's displayed.
    – John Scalo
    Commented Aug 17, 2014 at 22:12
  • 5
    As of 2014-09-05 Xcode 6 beta 7 still fires awakeFromNib every time the view is shown, even if scrolled off screen but Today sheet still visible. Would like to know how it'll actually work <@Apple>. Commented Sep 5, 2014 at 13:50
  • 8
    The release notes for iOS 8.1 beta clarify it a bit: The schedule and intended use of widgetPerformUpdateWithCompletionHandler: is intended as a convenient home for all data/model update logic. If implemented, the system will call at opportune times for the widget to update its state, both when Notification Center is visible, as well as in the background. An implementation is required to enable background updates. It’s expected that the widget will perform the work to update asynchronously and off the main thread as much as possible. Commented Sep 30, 2014 at 10:52
  • 2
    I have observed that on 8.0.2, Xcode 6.1 (6A1046a), on iPhone 5 (device, not simulator), the widget is being instantiated, loadView, viewDidLayoutSubviews, viewWillAppear, and viewDidAppear are all called every time the Notification Center is opened. When scrolling the widgets, I see ViewWillAppear and ViewDidAppear being called. I have yet to observe widgetPerformUpdateWithCompletionHandler being called. Leaving the notification center open for an extended period (15 minutes or so), leaving it closed (again, 15 minutes or so); in neither case does this method ever get called in my testing. Commented Oct 17, 2014 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.