iOs Widget background update

DigitalBrain_DEV picture DigitalBrain_DEV · Feb 19, 2015 · Viewed 9k times · Source

According to what apple says about widgetPerformUpdateWithCompletionHandler:, the iOs Widget can update his content also when the widget is not visible.

In my debug session i've noted that the todayViewController is deallocated everytime the notification center view disappear, so my questions are:

How my todayViewController can respond to widgetPerfomrUpdateWithComplationHandler if it is deallocated ?

I need that every hour the widget update his content also if it is not visible, how can i do that?

How is the iOs widget lifecycle?

Help me please :) Thanks

Answer

Midhun MP picture Midhun MP · Feb 19, 2015

The widget life-cycle is managed by the iOS itself.

When you go to background, the system takes a snapshot of your today's widget. The system manages the background fetch (background life cycle of your widget), it'll call the widgetPerfomrUpdateWithComplationHandler: for updating your widget data whenever possible and takes a snapshot each time. And when you open it again, it'll load the most recent snapshot first and loads the live data after that.


Widget Content Update

Updating Content

The Today extension point provides API for managing a widget’s state and handling updates to its content (you can read about this API in the Notification Center Framework Reference). Although there are a few platform-specific differences in the Today API, the functionality supported on both platforms is mostly the same.

To help your widget look up to date, the system occasionally captures snapshots of your widget’s view. When the widget becomes visible again, the most recent snapshot is displayed until the system replaces it with a live version of the view.

To update a widget’s state before a snapshot is taken, be sure to conform to the NCWidgetProviding protocol. When your widget receives the widgetPerformUpdateWithCompletionHandler: call, update your widget’s view with the most recent content and call the completion handler, using one of the following constants to describe the result of the update:

NCUpdateResultNewData—The new content required you to redraw the view

NCUpdateResultNoData—The widget doesn’t require updating

NCUpdateResultFailed—An error occurred during the update process

Reference Today Widget


Notification Center Data Updating

Notification Center

Note

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. Widgets should call the argument block when the work is complete, passing the appropriate NCUpdateResult. Widgets should NOT block returning from viewWillAppear: on the results of this operation. Instead, widgets should load cached state in viewWillAppear: in order to match the state of the view from the last viewWillDisappear:, then transition smoothly to the new data when it arrives.

Reference iOS 8.1 Notification Center


Widget Life Cycle

Widget Life Cycle

Reference: An App Extension’s Life Cycle