2

I'm writing code to a new iOS 8 Today widget, but I noticed that each time that widgetPerformUpdateWithCompletionHandler: is called my ivars (created from @property) are reset. It's is like every time a new view controller is getting instantiated.

This makes it impossible to save data on memory between updates to the widget (while it is in the background, for example, and is called to update its content).

Is this normal behaviour, or a bug? Should I save my simple numbers to NSUserDefaults instead of relying on memory based data, which is being reset?

jscs
  • 63,694
  • 13
  • 151
  • 195
sidyll
  • 57,726
  • 14
  • 108
  • 151
  • I haven't looked into this too thoroughly, but my understanding is that extensions' lifetimes as live processes are _extremely_ short -- no longer than absolutely necessary to perform their single task. See, e.g., [the "Life cycle" diagram in the docs](https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionOverview.html#//apple_ref/doc/uid/TP40014214-CH2-SW2). – jscs Oct 09 '14 at 02:20
  • Thanks @JoshCaswell . I saw that section but while it makes sense for various extension types, it seems strange to me that a widget lifecycle ends when the panel is closed (seems to be happening so). If every time that the panel is opened a new widget is instantiated, then I need to study the purpose of that method again. – sidyll Oct 09 '14 at 02:29
  • Your extension will _not_ be running in between calls to `widgetPerformUpdateWithCompletionHandler:`. That method is called when iOS launches your extension in the background for you to fetch new data. The OS then captures an image of your extension (thats what the completion handler is for) to show as a sort of "launch screen" for your extension (when notification center is launched your extension isn't available _immediately_ so it shows the image until it is). You likely want to use `NSUserDefaults` to store cached data to load while waiting for updated data to come from a server. – Andrew Oct 09 '14 at 02:50
  • In other words, the OS will launch your app periodically to let you fetch new data so that the user will always see updated data in notification center. You should cache this data in that method so that you can load your extension faster when it is launched for notification center. This is all discussed [here](https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/NotificationCenter.html#//apple_ref/doc/uid/TP40014214-CH11-SW6) – Andrew Oct 09 '14 at 02:54
  • @SantaClaus would you mind posting these comments as an answer? This is the exact information I was looking for. Thank you – sidyll Oct 09 '14 at 12:28

1 Answers1

0

Your extension will not be running in between calls to widgetPerformUpdateWithCompletionHandler:. That method is called when iOS launches your extension in the background for you to fetch new data. The OS then captures an image of your extension (thats what the completion handler is for) to show as a sort of "launch screen" for your extension (when notification center is launched your extension isn't available immediately so it shows the image until it is). You likely want to use NSUserDefaults (or another method) to store cached data to load while waiting for updated data to come from a server.

In other words, the OS will launch your app periodically to let you fetch new data so that the user will always see updated data in notification center. You should cache this data in that method so that you can load your extension faster when it is launched for notification center. This is all discussed here.

Andrew
  • 15,357
  • 6
  • 66
  • 101
  • 4
    Not quite accurate. Your extension *might* continue to run in between invocations to ```widgetPerformUpdateWithCompletionHandler:```. The reason the "ivars are reset" is because the UIViewController is (well, again, might— the behavior varies depending on system conditions) reinstantiated. Basically, don't assume that init calls to your main VC will be paired with the launching of your extension. They might be 1-to-1, to you might get instantiated multiple times in a launch. The extension might die when Notification Center goes away, but it might not. – John Scalo Oct 30 '14 at 19:30