iOs小部件背景更新 [英] iOs Widget background update
问题描述
根据苹果对widgetPerformUpdateWithCompletionHandler:的说法,当小部件不可见时,iOs小部件也可以更新他的内容。
在我的调试会话中,每当通知中心视图消失,todayViewController被释放,所以我的问题是:
如果我的todayViewController可以响应widgetPerfomrUpdateWithComplationHandler如果它被释放?
我需要每个小时更新他的内容,如果它是不可见的,我该怎么做呢?
iOs小部件生命周期?
帮助我:)
感谢
小部件生命周期由iOS本身管理。
当您转到背景时,系统会拍摄您今天的小部件的快照。系统管理后台抓取(窗口小部件的后台生命周期),它会调用 widgetPerfomrUpdateWithComplationHandler:
尽可能更新窗口小部件数据,并每次都拍摄快照。
当您再次打开它时,它会首先加载最新的快照,然后加载实时数据。小部件内容更新>
更新内容
今天的扩展点提供API来管理窗口小部件的状态
并处理其内容的更新(你可以在
中阅读关于这个API的Notification Center Framework Reference)。虽然在Today API中有一些
平台特定的差异,两个平台上支持的功能
大体上是一样的。
您的窗口小部件是最新的,系统偶尔会捕获窗口小部件视图的
快照。当窗口小部件再次变为可见的
时,显示最近的快照,直到系统用视图的活动版本替换
。
在拍摄快照之前更新窗口小部件的状态,确保
符合NCWidgetProviding协议。当你的小部件收到
的widgetPerformUpdateWithCompletionHandler:调用,更新你的
小部件的视图与最新的内容,并调用完成
处理程序,使用以下常量之一来描述结果
的更新:
NCUpdateResultNewData
- 新内容需要您重新绘制视图
NCUpdateResultNoData
- 小工具不需要更新
NCUpdateResultFailed
- 更新过程中发生错误
更新内容
今天的扩展点提供API来管理窗口小部件的状态
并处理其内容的更新(你可以在
中阅读关于这个API的Notification Center Framework Reference)。虽然在Today API中有一些
平台特定的差异,两个平台上支持的功能
大体上是一样的。
您的窗口小部件是最新的,系统偶尔会捕获窗口小部件视图的
快照。当窗口小部件再次变为可见的
时,显示最近的快照,直到系统用视图的活动版本替换
。
在拍摄快照之前更新窗口小部件的状态,确保
符合NCWidgetProviding协议。当你的小部件收到
的widgetPerformUpdateWithCompletionHandler:调用,更新你的
小部件的视图与最新的内容,并调用完成
处理程序,使用以下常量之一来描述结果
的更新:
NCUpdateResultNewData
- 新内容需要您重新绘制视图
NCUpdateResultNoData
- 小工具不需要更新
NCUpdateResultFailed
- 更新过程中发生错误
参考
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
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
Reference: An App Extension’s Life Cycle
这篇关于iOs小部件背景更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!