找到你要的答案

Q:WatchKit Glances Lifecycle call when back from energy save off?

Q:watchkit地生命周期打电话的时候,从节约能源吗?

I have a Watchkit Glance that I update using MMWormwhole that uses Darwin Notifications. Everything is working well as long as the Glance "is on", I mean active / shown - before the Apple Watch "turns it off" because of energy saving so that the Watch screen is black / off (I hope you get me ;)...

When you press the Crown the Glance is back showing, but if something has changed while it was "off", it does not get notified anymore and therefore didn t update...

So is there any lifecycle call that the Glance gets before "waking up from energy saving off mode" so that I could update the UI again? Something like "back from background mode". I know there is no backgrounding for watch... I couldn t find anything...

Any workaround or something I could do?

Thanks !

我有一个watchkit便知我更新使用mmwormwhole使用达尔文的通知。一切都很好,只要看“是”,我的意思是积极/显示-在苹果手表“关闭”,因为节能,使手表屏幕是黑色/关闭(我希望你能得到我;)…

当你按下皇冠的一瞥回来显示,但如果有什么改变,而它是“关闭”,它不会得到通知,因此没有更新…

因此,有没有生命周期调用,一目了然之前得到“醒来从节能关闭模式”,以便我可以再次更新的UI?类似“后台模式”。我知道没有后台看…我找不到任何东西…

我能做的任何工作或什么的?

谢谢!

answer1: 回答1:

for life cycle , you can check this blog

When multiple page-based interface controllers are loaded (whether they’re the root interfaces or they’ve been presented modally), init and awakeWithContext: are called on all controllers before willActivate is called on the first page. This means that you can improve the apparent responsiveness of your app by doing as little work as necessary in these two methods. As it’s a common question, be aware that awakeWithContext: will not be called again.

While there are no hard-and-fast rules about the work you should perform in init vs. awakeWithContext:, awakeWithContext: is the earliest that you have access to the context. Because of this, you could decide to perform general initialization in init and more context-specific work in awakeWithContext:. Note that you can configure interface elements in both of these methods.

After an interface controller is initialized and passed its context, the only time it can update its interface is while the interface is active. This means that you can perform updates in willActivate (when the interface becomes active) and at any point until didDeactivate is called. You cannot, however, update the interface within didDeactivate, as the interface is no longer considered active.

The following diagram illustrates two page-based interface controllers running on actual Apple Watch hardware. I mention this, because the NSExtensionContext notifications that you see here are not delivered in the simulator.

After the app starts, you can see that both interface controllers are given a chance to initialize and configure their contexts. It’s important to note that all of the calls in this diagram occur on the main thread.

Next, you see two NSExtensionContext notifications, >NSExtensionHostWillEnterForegroundNotification and >NSExtensionHostDidBecomeActiveNotification. Normally, these two notifications are very reliable. In my testing, though, I’ve noticed that—even if you register as an observer in your interface controller’s initialize class method—you might not catch these first two notifications. So, you need to plan accordingly.

Next, I swipe to Page 2. Note that willActivate is called on the second interface controller before didDeactivate is called on the first. This is contrary to the following statement in the Apple Watch Programming Guide:

During a transition, the currently visible interface controller’s didDeactivate method is called, followed by a call to the willActivate method of the interface controller that is about to be displayed.

This bit of documentation appears to be incorrect or out-of-date based on my experience with both the simulator and actual hardware.

Side note: This same sequence occurs when presenting a modal interface. That is, willActivate is called on the presented controller, followed by a call to didDeactivate on the presenting controller. When the modal interface is dismissed, again, willActivate is called on the presenting controller, followed by a call to didDeactivate on the presented (now dismissed) controller. Finally, as you’d expect, if there is no longer a strong reference to the now-deactivated controller, it is deallocated (from a different thread, by the way).

Back to our two-page example. If I drop my arm, you can see that two notifications are delivered: NSExtensionHostWillResignActiveNotification and NSExtensionHostDidEnterBackgroundNotification. Last, didDeactivate is called on the second interface controller before the app is suspended (the Watch does reserve the right to terminate your app).

After waiting a few seconds, I raise my arm to wake up the app, and you can see the two NSExtensionContext notifications before willActivate is called on our second interface controller. This pattern continues as controllers are pushed, popped, presented, and dismissed.

The life cycle that we’ve explored here is the same for hierarchical interfaces, Glances, and Notification interfaces, so you can take advantage of these events in all of those situations.

对于生命周期,你可以查看这个博客

当多个基于页面的接口控制器的负载(无论他们是根接口或他们已经提出模态),init和awakewithcontext:呼吁所有控制器之前将激活被称为第一页上。这意味着你可以通过在这两种方法中尽可能少的工作来提高应用程序的表观响应性。这是一个普遍的问题,要知道awakewithcontext:不再来。

虽然没有硬性规定,你应该在工作和awakewithcontext init执行:,awakewithcontext:是你已经进入中最早的。正因为如此,你可以决定执行初始化init和更多的背景一般在awakewithcontext具体工作:。注意,您可以在这两种方法中配置接口元素。

当接口控制器初始化并传递其上下文时,唯一可以更新接口的时间是在接口处于活动状态时。这意味着你可以在将激活执行更新(当界面变得活跃)和任何点到diddeactivate叫做。你不能,但是,在diddeactivate更新界面,当界面不再被认为是积极的。

下图说明了两个基于页的接口控制器在实际的Apple Watch硬件上运行。我提到这一点,因为nsextensioncontext通知你看到这里是不是在模拟器交付。

应用程序启动后,您可以看到两个接口控制器都有机会初始化和配置它们的上下文。需要注意的是,此图中的所有调用都发生在主线程上。

接下来,你看到两nsextensioncontext通知,nsextensionhostwillenterforegroundnotification和>;>;nsextensionhostdidbecomeactivenotification。通常情况下,这两个通知是非常可靠的。然而,在我的测试中,我注意到,即使你在接口控制器的初始化类方法中作为一个观察者注册,也可能无法捕捉到前两个通知。所以,你需要相应的计划。

接下来,我刷到第2页。请注意,将激活被称为第二接口控制器在diddeactivate是第一。这是违背了下面的声明在Apple Watch编程指南:

在转型期,当前可见的接口控制器的diddeactivate方法,接着调用的接口控制器,即将显示的将激活方法。

这一点的文件似乎是不正确的或过时的基础上我的经验与模拟器和实际硬件。

注意:当呈现模态界面时发生相同的顺序。即,将激活被称为控制器,接着一个电话diddeactivate就呈现控制器。当模态界面被驳回,再次,将激活被呈现的控制器,接着一个电话diddeactivate上提出的(现在被解雇)控制器。最后,正如你所期望的,如果没有再到现在关闭控制器的强引用,这是释放(从不同的线程的方式)。

回到我们的两页的例子。如果我把我的手臂,你可以看到两个通知送达:nsextensionhostwillresignactivenotification和nsextensionhostdidenterbackgroundnotification。最后,diddeactivate叫做第二接口控制器应用程序之前,暂停(看是否有权终止您的应用程序)。

等待几秒钟后,我抬起我的手臂醒来的应用程序,你可以先将激活被称为第二接口控制器看到两nsextensioncontext通知。这种模式继续作为控制器被推,弹出,提出,并驳回。

我们在这里探索的生命周期是相同的层次接口,眼色和通知接口,所以你可以利用这些事件在所有这些情况。

ios  objective-c  swift  lifecycle  apple-watch