locked
When to use ActivatedDeferral

    Question

  • Does returning quickly from the activation event and using the ActivatedDeferral tear down the splash screen faster or what should we use it for?
    Friday, March 23, 2012 6:34 PM

Answers

  • The activation deferral object is exposed to give developers the ability to complete simple, asynchronous tasks during activation. When a deferral object is requested, activation will not complete until deferralObject.complete() is explicitly called by the app once initialization tasks are complete. For example, deferral gives your app the ability to asynchronously read settings from an app data file that may be needed in order to render the app's first view. Deferral can also be useful when inspecting the activation context, in case you wish to load a different page for different activation types.

    Consider the following example...a weather app serializes and stores a small amount of information about 5 locations that the user has added within the app. The app saves this content in an app data file. On launch, the app wishes to quickly read this data so that when deferralObject.complete() is called and the splash screen is dismissed, the user is viewing the the previously retrieved forecast for the default city. The user's other 4 cities will also already be listed in the app's navigation bar. Once the app completes deferral and the user reaches the first view, the app can then begin retrieving the latest forecast information for each city while showing actionable progress to the user. In this example, the advantage to using the deferral pattern is that the user can view the previous state of the app (instead of viewing a blank page) while the app fetches up-to-date content via the network.

    Remember, only simple operations should be performed using deferral. A few things to keep in mind:

    1. Any unhandled errors/exceptions during deferral will cause your app to crash, which produces a rather jarring user experience. Make sure you explicitly handle any errors/exceptions while using deferral.
    2. Make sure any logic you perform will always conclude with a call to deferralObject.complete(). For example, if you only call deferralObject.complete() in the success callback of a promise, then deferralObject.complete() will never be called if an error is ever encountered (this will cause your activation to timeout, also producing a poor user experience).

      BAD:
      getSomethingDuringDeferral.done(function(something) {
      // Use "something", then call complete()
      deferralObject.complete()
      });

      GOOD:
      getSomethingDuringDeferral.done(function(something) {
      // Use "something", then call complete()
      deferralObject.complete()
      }, function (error) {
      // Handle "error"
      deferralObject.complete()
      });
    3. No lengthy operations should be performed via deferral. If the logic you need to perform requires network calls or if you need to process many lengthy files, consider launching to an "extended" splash screen, then performing these operations. 

    Thanks,

    Justin Cooperman

    Friday, March 23, 2012 10:58 PM