The Imaginative Universal

Studies in Virtual Phenomenology -- @jamesashley

WP7 Tip: tombstoning simplified

August 26
by James Ashley 26. August 2010 12:58

In order to prepare your phone app to go into tombstone mode (to make it tomb-worthy) it is necessary to save off state data associated with your app.  My preference is to simply save off view models – but you may want to also save off model objects or even individual field values.

There are two dictionaries for doing this. 

IsolatedStorageSettings.ApplicationSettings is available to save persistent data between runs of the application (whether the application gets tombstoned or simply gets closed down).

PhoneApplicationService.Current.State allows one to save and restore data when the application gets tombstoned.  It is handy if we only want to save off data temporarily – for instance if we do not need it for a fresh run of the application. 

Since State and ApplicationSettings are both dictionaries that automatically handle serialization and deserialization for us, much of the underlying code we are required to write when using them is also very similar.

In my own applications, I use a set of utility classes to ease saving and restoring my application state.

Since I generally only want to use State and ApplicationSettings to save and restore data, I encapsulate the plumbing behind a very simple interface.

    public interface IDataStorage
    {
        bool Backup(string token, object value);
        T Restore<T>(string token);
    }

The concrete classes aren’t particularly sophisticated.  I use this for transient data storage:

    public class TransientDataStorage: IDataStorage
    {

        public bool Backup(string token, object value)
        {
            if (null == value)
                return false;

            var store = PhoneApplicationService.Current.State;
            if (store.ContainsKey(token))
                store[token] = value;
            else
                store.Add(token, value);

            return true;
        }

        public T Restore<T>(string token)
        {
            var store = PhoneApplicationService.Current.State;
            if (!store.ContainsKey(token))
                return default(T);

            return (T) store[token];
        }
    }

And this for isolated storage:

    public class PersistentDataStorage: IDataStorage
    {

        public bool Backup(string token, object value)
        {
            if(null == value)
                return false;

            var store = IsolatedStorageSettings.ApplicationSettings;
            if (store.Contains(token))
                store[token] = value;
            else
                store.Add(token,value);

            store.Save();
            return true;
        }

        public T Restore<T>(string token)
        {
            var store = IsolatedStorageSettings.ApplicationSettings;
            if (!store.Contains(token))
                return default(T);

            return (T) store[token];
        }
    }

You’ll notice that the only real difference between these two code segments is that I need to call Save for isolated storage but do not have to for State updates.

Now when I need to save to isolated storage I simply call:

    var store = new PersistentDataStorage();  
    store.Backup("token", myObject);

While a transient save to State is simply:

    var store = new TransientDataStorage();  
    store.Backup("token", myObject);

No sticky kids mess afterwards.

I like having an interface as it allows me to throw some methods on the VMs themselves:

public static bool Backup(IDataStorage store)
{
    return store.Backup("MainViewModel", this._instance);
}

public static void Restore(IDataStorage store)
{
    this._instance = store.Restore<MainViewModel>("MainViewModel");
}

In this case, each VM is responsible for what actually gets saved to storage: for instance, we might want to save off the VM as well as the  associated model object. 

Whether this state is saved off to IsolatedStorage or State, however, gets determined elsewhere – generally in the ApplicationService lifecycle events: Launching, Closing, Activated and Deactivated.

Tags: , ,

Windows Phone | Silverlight

Comments

8/29/2010 3:14:17 PM #

trackback

Windows Phone 7–Experiência para programadores, XXXV

Depois de uns dias fora com a família, o volume de links para artigos relacionados com o desenvolvimento

Alberto Silva

9/5/2010 6:40:37 PM #

pingback

Pingback from cisforcoder.wordpress.com

Windows Phone 7 Developer Links « C Is For Coder

cisforcoder.wordpress.com

9/6/2010 10:58:29 PM #

pingback

Pingback from dotnetcatch.wordpress.com

WP7 – Tombstoning simplified, less duplication « dotnet Catch

dotnetcatch.wordpress.com

10/12/2010 5:40:28 PM #

Brad Tutterow

Very nice.  Making the Restore method generic is a particularly nice touch.

Brad Tutterow United States

10/17/2010 3:15:54 AM #

apple

Carpet Cleaning - Removal of Pet Dander and Odor in Carpets.....A pet for several people is equal to having an extended member of the family unit.....It can be joyful learning your pet's character and watching them grow.....Regrettably, the price of a pet goes further than the price you paid in the pet store.....

apple United States

10/27/2010 6:54:24 AM #

trackback

Дайджест технических материалов #5 (Windows Phone 7)

Tools, Books, Guides Windows Phone 7 Developer Tools RTM (online installer) , ISO UI Design and Interaction

Oleksandr Krakovetskiy blog

11/27/2010 5:55:38 AM #

trackback

Attribute-based transient Tombstoning

Attribute-based transient Tombstoning

Mister Goodcat

12/9/2010 6:05:57 AM #

amir khan vs marcos maidana

This fight of Khan vs Maidana will be on December 11. Let us watch it live.

amir khan vs marcos maidana United States

1/3/2011 1:59:47 PM #

carpet cleaning bellevue

Looks like an interesting story, just wish I was techie enough to understand it.

carpet cleaning bellevue United States

1/28/2011 11:01:04 PM #

carpet cleaning dallas

thats saved me a bunch of time with that code ,

carpet cleaning dallas United States

1/28/2011 11:03:00 PM #

carpet cleaning atlanta

the hardest part of tombstoning is to get that application to work properly but with this code it just made it a lot easier , but I still dont always do it right ugghhhh

carpet cleaning atlanta United States

3/23/2011 2:44:24 AM #

instyler

You are a very smart person!

instyler People's Republic of China

5/24/2011 8:54:48 AM #

pingback

Pingback from blogs.techrepublic.com.com

Windows Phone 7 tombstoning explained | TechRepublic

blogs.techrepublic.com.com

6/8/2011 5:18:06 AM #

Pandora Bracelets

Carpet Cleaning - Removal of Pet Dander and Odor in Carpets.....A pet for several people is equal to having an extended member of the family unit.....It can be joyful learning your pet's character and watching them grow.....Regrettably,

Pandora Bracelets United States

6/21/2011 12:31:19 PM #

pingback

Pingback from chaokuaidi.com

超快递 beta版 » Windows Phone 7 资源汇总(超全)

chaokuaidi.com

6/30/2011 10:02:27 AM #

fabric blinds

nice post on programming

fabric blinds United States

7/1/2011 9:48:53 PM #

monster energy hats

Recommend you to take a look at it, is now being discounted, Monster Energy Hats Collection in hot! Fashionable dc shoes hats Keeps with you at any time.

monster energy hats People's Republic of China

7/3/2011 9:38:28 PM #

Pandora Bracelets

Good!Dear friends, thank you for visiting our website ,we are an international trade company . We at competitive price,providing a huge range of Jewellery。 You can buy cheap Pandora Jewellery. Welcome to visist here.

Pandora Bracelets People's Republic of China

5/17/2012 11:10:20 PM #

pingback

Pingback from dancecoder.com

Windows Phone 7 资源汇总 | DanceCoder

dancecoder.com

8/4/2013 2:25:13 AM #

pingback

Pingback from com-lab.biz

How to save the state of CustomObject state in wp7 phone ap | user83

com-lab.biz

Comments are closed