Flex 4.5 PersistenceManager
The PersistenceManager is the data center backbone to your mobile Flex application. The reason you need to think about the persistance layer of your mobile app more than you needed to before in web and desktop development is because of the high probability of interruption and application closure.
In web and desktop development if something else happened on the user’s computer the user could simply switch windows to handle the interruption and continue their task. This happens so frequently that developers actually never think of this as a issue. However with mobile development there is always the chance of a phone call coming in, or email, or any other application that may take focus away from your application. While you application is not in immediate use you now fall prey to the operating system’s whim. If the OS decides that your application needs to close for memory or power reasons then the state of your application and any data it was holding may now be lost unless you have created an aggressive data persistance layer.

On top of that, the views within your application are created and destroyed quite frequently. That means if you were a developer that was used to grabbing data from offscreen or invisible views then your data will no longer be available when these other views are destroyed. Another reason why you need to be very aggressive about moving data into a proper data model and away from the view.
Getting into it let’s look at the PersistenceManager, what it is, and how we can alter it to do our bidding.
PersistenceManager
First off the PersistenceManager is a simple way to interact with an LSO (Local Shared Object). This is the default way to persist data across views and sessions. There are only a handful of functions to worry about to get use this class.
//
// Methods
//
//--------------------------------------------------------------------------
/**
* Clears all the data that is being stored by the persistence
* manager.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
function clear():void;
/**
* Flushes the data being managed by the persistence manager to
* disk or another external storage file.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
function flush():void;
/**
* Initializes the persistence manager.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
function initialize():void;
/**
* Returns the value of a property stored in the persistence manager.
*
* @param key The property key
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
function getProperty(key:String):Object;
/**
* Stores a value in the persistence manager.
*
* @param key The key to use to store the value
* @param value The value object to store
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
function setProperty(key:String, value:Object):void;
This makes it very easy to set and get data from the persistence layer.
p.setProperty("key", 1232131.123);
...
...
var p:PersistanceManager = new PersistenceManager(); /* this is a singleton */
var key:Number = p.getProperty("key") as Number;
Roll Your Own
Don’t like using LSOs? Prefer SQLite Databases or some other storage plan? No problem. You don’t have to roll your own completely if you don’t want to – though you could. You can still use the PersistenceManager, just make sure to use the interface: IPersistenceManager. The functions are all the same but now you can make your own and do whatever you want. This is a wonderful example of programming to an interface rather than an implementation.
While the simple functions provided by the IPersistenceManager may not be robust enough for your application then you can just completely write your own class. I prefer to stick to the provided interfaces because then other developers that help out don’t have to learn an entire new set of methods.
Looking at getting started with Flash Builder Burrito and the new Flex SDK? Check out this post on Flex Hero development.
Looking at getting going with non-Hero development or don’t have the new Flash Builder Burrito? Check out this post on getting started with Air for Android.






[...] This post was mentioned on Twitter by Jonathan Campos, Brian Rinaldi, Jay Proulx ?, killerspaz, Frédéric DE MATOS and others. Frédéric DE MATOS said: Flex 4.5 PersistenceManager | The World In A State of Flex: http://goo.gl/D0SL [...]
[...] Cela peut se révéler très utile lors du développement d’applications mobiles. Consulter l’article Les nouveautés de Flash Builder BurritoLa nouvelle version du Flash Builder met [...]
[...] Follow this link: Flex 4.5 PersistenceManager | UnitedMindset [...]
[...] PersistenceManager alleviates the hassle of trying to manage data between view states by using Local Shared Objects. Even when your mobile OS decides to close your app for memory or power shortages, the PersistenceManager will preserve your designated data. Check Jonathan Campos’s post on it for more details: Flex 4.5 PersistenceManager [...]
Thanks for this blog post. This is exactly what I need for my Playbook app.
John,
Does the persistence manager need to be initialized?
p.initialize();
Matt,
Nope, just say new PersistenceManager and you should be good to go.
[...] Flex 4.5 PersistenceManager by Jon Campos - http://www.unitedmindset.com/jonbcampos/2010/11/01/flex-4-5-persistencemanager/ [...]
Let’s imagine I’ve got a Flex app that uses PersistenceManager for a tablet (Android or iOS based). If the application was to be updated (e.g. new version published through AppStore or Market), does anyone know if any of the values stored through PersistenceManager would be lost after the update?
Cheers,
Bashar
@Bashar
As someone that uses this often and also has put out lots updates I can tell you that the data remains after updates. Though if they uninstall the data is gone.
Thanks for the informative article. This does a good job of clearing up what the PersistenceManager is and what it is not. There is, however, one small problem. It’s a library component. You need a library to use it. This document does not specify the name of the library file or where this file can be found. Without this information, it is not possible to use this component.
@Jeff PersistenceManager is in the Flex framework for mobile applications. Specifically in the mobilecomponents.swc.
HTH
@Jeff
import spark.managers.PersistenceManager