iOs Widget background update
Asked Answered
A

3

12

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

Armenian answered 19/2, 2015 at 10:40 Comment(0)
A
18

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

Autotransformer answered 19/2, 2015 at 11:8 Comment(6)
So i can't update the content while the widget is not visible? Apple says :" 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." But in my debug i've seen that widgetPerformUpdate... is never called when the NotificationCenter is in backgroud at least for my widget).Armenian
here in Notification Center section :developer.apple.com/library/ios/releasenotes/General/…Armenian
@digitalPad: yes, the system will invoke the notification center and it'll invoke your widget also. But you can't handle those parts by your own. It's system managed and system handled. (Something like background fetch)Autotransformer
ok thank you, so i can't be sure that the method is called early early enough. Is there another way to update the widget contents in background? i need that every hour the widget download a new feed so every time the user see the widget it is updatedArmenian
@digitalPad: There is no way AFAIK. You can check on the background fetch (but it's for the app, it's also system handled)Autotransformer
It says "An implementation is required to enable background updates", thats very clear. Anyways, how is that done?Freytag
A
4

iOS8 release notes

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.

You need to implement both

  • viewDidAppear:
  • widgetPerformUpdateWithComplationHandler:

Refresh your data in widgetPerformUpdateWithComplationHandler: then call the completion block with NCUpdateResult.

Refresh your view in viewDidAppear: The launch image (snapshot) will be from your last viewDidDissappear: New contents will be displayed at the end of the next run loop.

Credit to DigitalBrain_Dev for document reference.

Andrel answered 19/4, 2016 at 19:43 Comment(0)
G
-1

Call your API in func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) method and call the completionHandler(NCUpdateResult.newData) .

Gentlefolk answered 27/6, 2019 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.