State vs IsolatedStorage

Dec 30, 2011 at 3:31 PM

Hi again.

I think your library is great and I think I'm going to use it. But I noticed you're using the IsolatedStorage to save the data. This can be handy for storing large amounts of data, but the default scenario should be saving the data to PhoneApplicationService.Current.State.

From MSDN:
The State dictionary of the PhoneApplicationService is a temporary storage location that persists only as long as your application is tombstoned, but is much quicker to access than persistent storage. Using these two storage types correctly will have a big impact on the user experience and load time of your application.

So it might be nice to change the default implementation for your library.


Dec 30, 2011 at 10:18 PM

Hi mpost, thanks for both comments, I'll look at implementing the first one, I chose isolated storage because I wanted better control over serialization with complex types, it would be possible to use current state if the complex objects were serialized to a string first - I'll revisit this! Thanks again.

Dec 31, 2011 at 10:13 AM

Hi Geoff,

I did some testing and you're right. Using the PhoneApplicationService.Current.State directly throws errors when using complex types. That's not really usable.

I also did a quick test using the Current.State and serializing to a string first. That seems to work really good. My test (on a real device) also shows this is 50% faster than using Isolated Storage. Here is the code I used for the IsolatedStorage class:

public static bool StoreObject<T>(string key, T o, bool overwrite)
    bool stored = false;

    if (overwrite || !PhoneApplicationService.Current.State.Keys.Contains(key))
        using (StringWriter sw = new StringWriter())
            XmlSerializer xs = new XmlSerializer(typeof(T), KnownTypes);
            xs.Serialize(sw, o);

            string serializedXml = sw.ToString();

            PhoneApplicationService.Current.State[key] = serializedXml;
            stored = true;


    return stored;

public static T GetObject<T>(string key)
    T o = default(T);

    if (PhoneApplicationService.Current.State.Keys.Contains(key))
        string data = (string)PhoneApplicationService.Current.State[key];

        XmlSerializer xs = new XmlSerializer(typeof(T), KnownTypes);
        o = (T)xs.Deserialize(XmlReader.Create(new StringReader(data)));

    return o;

public static void RemoveObject(string key)
    PhoneApplicationService.Current.State[key] = null;

Dec 31, 2011 at 4:36 PM

Great work, I'll get this change put in. This also means the app will terminate faster as there is no need to clear the storage as it gets binned anyway.