Windows Phone – Contact Data Cannot Persist in Isolated Storage

Last few days, I was trying to develop a new project that involve saving some Contact data from phone to isolated storage or IsolatedStorageSettings. The reason behind this , I want the app to keep track of a list of favorite contacts. Everything went smooth until the part saving the contacts into isolated storage. It was a nightmare. Saving a copy of contact data into isolated storage did not give me any warning or problem at all. However after I quit the app and restart, the app fail to retrieve any contact data from isolated storage. It look like the app never successful save any data to the isolated storage.

 

public List<FavoriteContact> LoadFavoriteContactsFromStorage()
{
    try
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains("FavoriteContacts"))
        {
            List<FavoriteContact> tempFavoriteContacts;
            if (IsolatedStorageSettings.ApplicationSettings.TryGetValue<List<FavoriteContact>>("FavoriteContacts", out tempFavoriteContacts))
            {
                return (tempFavoriteContacts);
            }
        }
    }
    catch
    {

    }

    return (new List<FavoriteContact>());
}

public void SaveFavoriteContactsToStorage()
{
    IsolatedStorageSettings.ApplicationSettings.Remove("FavoriteContacts");
    IsolatedStorageSettings.ApplicationSettings.Add("FavoriteContacts", FavoriteContacts);
    IsolatedStorageSettings.ApplicationSettings.Save();
}

 

At first, I though I did something wrong in load or save function related with isolated storage. After debugging the whole day, it turn out that the function work perfectly OK without any problem. It did not throw me a single error at all. After Bing here and there on the Internet, at last I found culprit of this problem. It is the Contact data itself. In theĀ Best Practices for Contact class, I found the following message:

Contact data is provided as a read-only snapshot. If your application needs fresh data, repeat the original search periodically.

Although the app did not throw any error, warning or exception, but I think it is not possible to direct save the Contact object into the isolated storage. Why I think instead of I’m very sure? This is because I can’t find any information related witht the same problem I’m facing, so I assume read-only Contact object can’t be saved. I replace the Contact class with another class I build with the similar properties with Contact class, then everything work nicely.

The conclusion here is you can’t save Contact object to isolated storage or IsolatedStorageSettings . No error, warning or exception if you do that, but it just don’t work and it will screw your whole day by letting you figure out what happen actually.

by Ooi Keng Siang via Ooiks’s Blog