DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Don’t Use PhoneApplicationService.State[] in Mango!

Social Ebola user avatar by
Social Ebola
·
Mar. 02, 12 · Interview
Like (0)
Save
Tweet
Share
3.43K Views

Join the DZone community and get the full member experience.

Join For Free

People who tell you, unequivocally, to not use a certain service in a platform are usually wrong. What they really mean to say is “don’t use service X under this and that circumstances”. However, like in politics, a simple bold statement gets more attention that a true one.

So here’s the more complete and actually true title of this post:

“Don’t use PhoneApplicationService.State[] in Mango for large pieces of data (say, 50k and more) or if the serialization/deserialization process of the data is complicated and may affect your application state”

If you do, you will use a bunch of memory you don’t really need and also cause your reactivation (when the user switches to your app, or if the user backs into it) to be slower.

Context

With Mango, and Fast Application Switching (FAS), there’s a subtle change in how applications behave on activation. To learn more about that, go to msdn – you will find nice pictures that will explain exactly what happens and what a dormant application is and how to properly handle the Activate event on the app.

History

Pre-mango, this was a useful mechanism by which you could save your transient data so that you can come back from tombstoning of your application (you can read about tombstoning in that article above). Essentially, in WP7.0, the application was given a chance to save its state so that it may come back to life when you “back” into it and look like you did not leave it. The three primary ways for doing this are page state (these are used to save objects that you want back when backing into a page), query parameters on a page uri (for primitives that do not go over a certain length – they will always be in the NavigationContext.Query property of the page) and PhoneApplicationService.State which is used for app-global state that needs to be resurrected when the app is activated.

So what changed with mango?

With Mango, for the most part, tombstoning no longer happens with your app (unless your navigation history is pretty darn big). Because applications are kept dormant in memory, there’s no need to dehydrate them using the various states we talked about (you probably still want to use them in the event that you ARE tombstoned though – you will probably not pass certification if you don’t). Which brings us to the broken bit here…

Mango will dehydrate your PhoneApplicationService.State saved objects regardless of whether you are going back to a dormant application or being rehydrated from a tombstoned state. To illustrate, take the following example:

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    PhoneApplicationService.Current.State["obj"] = m_test;
}

In here, when the app goes away and is getting deactivated, we go and save an object into the state. m_test is of the following type:

public class TestObject
{
    public TestObject()
    {
        TestInfo = "test";
    }
    public string TestInfo { get; set; }
}

Now, generally, this is how your “Activate” event callback should look like:

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    if (e.IsApplicationInstancePreserved)
    {
        return;
    }
 
    m_test = (TestObject)PhoneApplicationService.Current.State["obj"];
}

The first “if” is a Mango addition – if the app was just dormant, we don’t need to actively restore state. Great.

But here’s the rub. When you go back to the app, even if it was just dormant, a new TestObject will be constructed – even though it’s never going to be needed. If your saved state takes time to serialize/deserialize, this will affect reactivation of your app and will also take memory in the app that you are not really going to use.

Alternatives

Still with us? There aren’t a lot of good alternatives… If all you save in the state is a bunch of bools or even simple classes, feel free to continue doing that and ignore this whole post. However, if you store a lot of state in there, consider saving the state into isolated storage on deactivate – you will then have control over whether or not you need to read the data back.

Mango (software) app application

Published at DZone with permission of Social Ebola. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java REST API Frameworks
  • 5 Software Developer Competencies: How To Recognize a Good Programmer
  • File Uploads for the Web (2): Upload Files With JavaScript
  • How Chat GPT-3 Changed the Life of Young DevOps Engineers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: