iOS 8 Today Widget中widgetPerformUpdateWithCompletionHandler的用途是什么? [英] What is the purpose of widgetPerformUpdateWithCompletionHandler in iOS 8 Today Widget?

查看:729
本文介绍了iOS 8 Today Widget中widgetPerformUpdateWithCompletionHandler的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直在寻找答案,但一切看起来都模糊不清。

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

无论如何,只是想知道这个功能的用途:

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

widgetPerformUpdateWithCompletionHandler

今天的小部件。

根据 Apple

According to Apple:


调用此方法可为窗口小部件提供更新其
内容并在快照等操作之前重绘其视图的机会。
当窗口小部件完成更新其内容(并重绘,如果需要
)时,窗口小部件应调用完成处理程序块,
传递相应的NCUpdateResult值。

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.

快照何时发生​​?每当我调试扩展时,widgetPerformUpdateWithCompletionHandler总是在loadView之后被调用。那么当我已经在loadView中加载信息时,在此方法中显式重新加载信息的目的是什么?

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?


根据这个网站: http://www.karlmonaghan.com/tag/today-widget/


在TodayViewController中,有两个地方需要从网络加载
数据 - 创建小部件时当调用
widgetPerformUpdateWithCompletionHandler时。对于前者,I
在viewDidLoad中加载帖子,以便它们在小部件显示的
时间内准备就绪。当iOS认为小部件首次显示后显示为
时,会调用
widgetPerformUpdateWithCompletionHandler,为小部件提供
的机会来更新显示的帖子。

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.

与上述问题相同。

推荐答案

小部件是每次查看通知中心时都不会创建,因此每次显示时都不会调用 loadView 。当通知中心认为需要更新小部件信息时,它会调用 widgetPerformUpdateWithCompletionHandler 。从我自己的调试开始,它看起来就像最初创建窗口小部件时一样 widgetPerformUpdateWithCompletionHandler 几乎立即调用,所以你可以在那里完成所有加载,但Apple建议你启动加载过程尽可能早在生命周期中。

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.

如果您的窗口小部件显示的信息永远不会更改,那么您无需在 widgetPerformUpdateWithCompletionHandler 中执行任何操作。

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

这篇关于iOS 8 Today Widget中widgetPerformUpdateWithCompletionHandler的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆