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

查看:14
本文介绍了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

在今天的小部件中.

根据 苹果:

调用这个方法是为了给一个小部件一个更新它的机会内容并在快照等操作之前重绘其视图.当小部件完成更新其内容(和重绘,如果必要),小部件应调用完成处理程序块,传递适当的 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 被调用.对于前者,我在 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天全站免登陆