Search code examples
ioscocoa-touchios8ios8-extensionios8-today-widget

Are Today Extension Ivars Reset Constantly?


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?


Solution

  • 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.