Nov 2 2011

SwimEventTimes W7P Development -- Isolated Storage and Tombstoning

Category: � Administrator @ 17:10

At the outset of development I really didn't look into tombstoning and what that entailed but I knew I had to make use of Isolated Storage for keeping the content resident on the phone once it had been "SCRAPED" from the target URL.  You're asking yourself why am I talking about Isolated Storage and tombstoning together.  Well as it turn out, getting your app back to the state that it was in prior to tombstoning requires Isolated Storage.  I have designed my Isolated Storage with a GUID as its key:

Guid SwimmerGuid;
SwimmerGuid = Guid.NewGuid();

Once I have the data from the Target URL I generate a new GUID and that becomes the key that is passed from one page to the next.  In the "navigatedTo" pages, this is passed in the QueryString (think ASP.NET) and the rest of the data values are re-hydrated from Isolated Storage:

this.NavigationService.Navigate(new Uri("/MeetDetails.xaml?selectedGUID=" +
                    curSwimmer.SwimmerID + "&Meet=" + meetName, UriKind.Relative));

This makes easy work of adding new pages since you can construct your called page and know that you'll have the data available from Isolated Storage.

I'm talking about Isolated Storage as if I'm constantly hitting those files but in reality I simply read the stored data (into ObservableCollection) when the App starts up and I rewrite it when I add new data from the target URL:

private void Application_Activated(object sender, ActivatedEventArgs e)
        {
            this.LoadSavedApplicationState();
        }
void LoadSavedApplicationState()
 {
     if (Database.Instance.RequestedSwimmers == null)
     {
         using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
         {
             if (store.FileExists("RequestedSwimmers.xml"))
             {
                 using (IsolatedStorageFileStream stream = store.OpenFile("RequestedSwimmers.xml", System.IO.FileMode.Open))
                 {
                     XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<RequestedSwimmer>));
                     Database.Instance.RequestedSwimmers = (ObservableCollection<RequestedSwimmer>)serializer.Deserialize(stream);
                 }
             }
             else
             {
                 Database.Instance.RequestedSwimmers = new ObservableCollection<RequestedSwimmer>();
             }

        }

    }

When I need to write, I add new swimmers to the ObservableCollection and write it back to Isolated Storage:

if (Database.Instance.RequestedSwimmers != null)
         {
             using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
             {
                 using (IsolatedStorageFileStream stream = store.CreateFile("RequestedSwimmers.xml"))
                 {
                     XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<RequestedSwimmer>));
                     serializer.Serialize(stream, Database.Instance.RequestedSwimmers);

                 }
             }
         }

 

This way Isolated Storage is always up to date.

Resuming from tombstoning is straight forward since all the pages are called with a QuerySring (GUID) and the ObservableCollection has already been re-hydrated from Isolated Storage.

 

 

 

Tags: , , , , ,

Add comment