1 / 28

The story of state App data, settings, and the process lifecycle

The story of state App data, settings, and the process lifecycle. Kraig Brockschmidt Senior Program Manager, Windows Ecosystem Team Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript 3-126. Agenda. Look at stateful apps from the perspective of state itself

skylar
Télécharger la présentation

The story of state App data, settings, and the process lifecycle

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The story of stateApp data, settings, and the process lifecycle Kraig Brockschmidt Senior Program Manager, Windows Ecosystem Team Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript 3-126

  2. Agenda • Look at stateful apps from the perspective of state itself • What is its purpose • Where does it live • What affects and modifies state

  3. The purpose of state is to maintain a consistent app experience acrosssessions, devices, and process lifecycle events

  4. The user experience: apps are stateful • Apps don’t start in an uninitialized state, even on first run • Persistent settings are always in effect • Settings that are user-specific but not device-specific can roam across devices (like account setups) • The settings charm is where users manage relevant state • Transient session state (like unsubmitted form data and navigation history) is preserved across sessions if and only if Windows terminates an app • User data is more app-independent

  5. State is persistent • State exists when apps aren’t running or in memory at all • State carries user preferences across sessions • State carries transient session data across suspend, terminate, and restart • State can roam across a user’s devices • State can be modified by background tasks • State is versioned independently of apps (and less often)

  6. Review of process lifecycle events App gets 5 seconds to handle suspend App is not notified before termination Suspending Running app Suspended app Terminated app User launches app Low resources Resuming Apps are notified when they have been resumed Splash screen Code gets to run App frozen App not running Limited background tasks can run

  7. Stateful apps • Demo

  8. The big picture of state Running Suspended Not running System restart Other devices In memory(app changes variables) Local/temp app data (modified by WinRT and other APIs)Includes databases (SQLite, IndexedDB, ESE/Jet) and other facilities built on appdata (HTML AppCache, local storage, third-party libraries) Roaming app data (modified by WinRT and other APIs), sync’d to cloud (within quota) Windows.Storage.AccessCache (modified by WinRT API) Windows.Storage.PasswordVault (modified by WinRT API), sync’d to cloud

  9. using Windows.Storage; // Create a simple settingApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;localSettings.Values["message"] = "Hello World";Object value = localSettings.Values["message"]; // Create a setting in a containerApplicationDataContainer container = localSettings.CreateContainer("exampleContainer", ApplicationDataCreateDisposition.Always); localSettings.Containers["exampleContainer"].Values["message"] = "Hello World"; Object value = localSettings.Containers["exampleContainer"].Values["message"]; Basic state settings (C#)

  10. using Windows.Storage; // Write a fileStorageFolderroamingFolder = ApplicationData.Current.roamingFolder;StorageFile file = await roamingFolder.CreateFileAsync(filename,CreationCollisionOption.ReplaceExisting);awaitFileIO.WriteTextAsync(file, counter.ToString()); // Read a fileStorageFilefile = await roamingFolder.GetFileAsync(filename); string text = awaitFileIO.ReadTextAsync(file); Basic files (C#)

  11. using Windows.Storage; ApplicationDataContainer roamingSettings = ApplicationData.Current.RoamingSettings;ApplicationDataCompositeValuecomposite = newApplicationDataCompositeValue(); composite["readerSet"] = "Liam's Books"; composite["page"] = 524; roamingSettings.Values["HighPriority"] = composite; Composite + HighPriority roaming (C#)

  12. using Windows.Storage; // DataChanged is fired when new data has been roamed to this device applicationData.DataChanged += newTypedEventHandler<ApplicationData, object> (DataChangedHandler); asyncvoid DataChangedHandler(Windows.Storage.ApplicationData appData, object o) { // DataChangeHandler may be invoked on a background thread, so use the // Dispatcher to invoke the UI-related code on the UI thread. Not needed // in JavaScript. awaitthis.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Handle new data }); } DataChanged event (C#)

  13. using Windows.Storage; using Windows.Storage.AccessCache;// First permission granted by folder pickerStorageFolder folder = await folderPicker.PickSingleFolderAsync(); if (folder != null) { // Remember permission for future access to the folder // (including other sub-folder contents) StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder); } // Retrieve cached permission (in a later session)StorageFolder folder2 = awaitStorageApplicationPermissions.FutureAccessList .getFolderAsync("PickedFolderToken"); Access cache (C#)

  14. Credential Locker • For securely storing passwords apps should neither store nor roam passwords independently • On trusted PCs, these are roamed with the user • Also note CredentialPicker API • Sample:http://bit.ly/OUr8LC

  15. using Windows.Security.Credentials; using Windows.Security.Credentials.UI; CredentialPickerOptions credPickerOptions = newCredentialPickerOptions(); // Set options like captions, messages, protocol, etc. var res = awaitCredentialPicker.PickAsync(credPickerOptions); PasswordVault vault = newPasswordVault(); // Password typically encrypted already from UI...but this is good for plain text PasswordCredential c = newPasswordCredential("myCreds", res.CredentialUserName, res.CredentialPassword); vault.Add(c); // To retrieve later onPasswordCredential cred = vault.Retrieve("myCreds", userName); Credential Picker & Locker (C#)

  16. Additional AppData APIs • All languages • SQLite: http://bit.ly/MuzL1e (Tim Heuer’s blog) • ESE/Jet APIs (Win32): http://bit.ly/ToslcK (reference); for JavaScript needs a WinRT component • JavaScript only • IndexedDB: http://bit.ly/RyT9uk (reference) http://bit.ly/P5H292 (sample) • HTML5 localStorage: http://www.w3.org/TR/webstorage/ • HTML5 AppCache: http://dev.w3.org/html5/spec/offline.html

  17. State versioning • Applies to the entire contents of AppData • Managed through SetVersionAsync • No relationship to app version: many app versions can and will likely use the same version of state • On launch, app should migrate old state if found, and call SetVersionAsync to update the version • Cloud service for roaming data maintains multiple versions until all apps have upgraded • Can use ServicingComplete background task trigger to migrate state when an app update is installed

  18. Settings UI • Settings is a ubiquitous app feature, hence the charm • Settings charm eliminates need to have specific settings pages within the app’s navigation hierarchy • Use for state and configuration that the user can control • Accounts, preferences, etc. • Specifying what roams and what doesn’t • State that app maintains silently doesn’t need to appear here • App’s settings flyouts are just pieces of UI with unique means of invoking them; typically write to persistent state

  19. Settings API • App responds to Windows.UI.ApplicationSettings.CommandsRequested event • App populates commands • Links: help, privacy statement, terms of use, etc. • Panels: invoke flyouts that change app data • System provides permissions, rate and review • Settings flyout controls • XAML: Windows.UI.Xaml.Controls.SettingsFlyout • JavaScript: WinJS.UI.SettingsFlyout

  20. Settings • Demo

  21. Background tasks for state • Maintenance triggers • Run periodically on AC power • Useful for cleaning up temp state • System triggers • AC power, non-lock screen • InternetAvailable, NetworkStateChange for connectivity • ServicingComplete: perfect time to migrate app state versions • Lock screen triggers (AC or battery power) • Session Connected, UserPresent, UserAway, TimeTrigger • All tasks subject to CPU and network activity quotas • Independent of main app (can use mixed languages)

  22. Background tasks for state • Demo

  23. Best practices for apps • Launch with initial defaults • JavaScript: use sessionState object as a namespace for variables • C#/VB/C++: Use SuspensionManager helpers • Write session and persistent state incrementally as it changes • Don’t leave this for the suspending event unless necessary • Always save file references in access cache—never save paths • Some files/folders don’t come from the file system! • Always save passwords in the Credential Locker • Use encryption for security, compression to minimize size

  24. Best practices for apps • Session state: restore if launched after terminated • Typically only a few pieces of data • Session state is not restored if app is launched to service contracts or launch arguments • Exception: search • Launch arguments means file type activation or secondary tile • Check elapsed time and refresh when resuming • Especially data from online services

  25. Related sessions • 10/31 145p – Kodiak – Introduction to creating Windows Store apps using XAML (3-116) • 10/30 215p – Kodiak – Introduction to creating a Windows Store App using HTML and JavaScript (3-115) • 11/1 830a – Kodiak – Alive with activity: Tiles, notifications, and background tasks (3-101)

  26. Resources • Application data sample • Launching, resuming, and multitasking • Background task sample • Programming Windows 8 Apps in HTML, CSS, and JavaScript

  27. Resources • Develop: http://msdn.microsoft.com/en-US/windows/apps/br229512 • Design: http://design.windows.com/ • Samples: http://code.msdn.microsoft.com/windowsapps/Windows-8-Modern-Style-App-Samples • Videos: http://channel9.msdn.com/Windows Please submit session evals by using the Build Windows 8 app or at http://aka.ms/BuildSessions

More Related