O'Reilly logo

ActionScript 3.0 Cookbook by Joey Lott, Darron Schall, Keith Peters

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Saving a Local Shared Object

Problem

You want to save data in an LSO to the client computer.

Solution

Use the SharedObject.flush() method.

Discussion

Flash automatically attempts to save LSO data to disk when either the movie is unloaded from the Player (such as when the Player closes), when the SharedObject instance is garbage collected (which occurs when there are no longer any references to the instance) or when the SharedObject.clear() method is invoked. However, it isn’t a good practice to rely on the automatic save function, as there are several reasons why the data might not be written to disk successfully. Instead, you should explicitly instruct the LSO to write the data to disk using the SharedObject.flush() method:

var flushResult:String = example.flush();

The flush() method takes an optional parameter specifying the minimum amount of disk space, in bytes, that should be used when writing the shared object. The value specified defaults to 0, meaning that only the minimum required space should be used to write the local shared object to disk.

When the flush() method is invoked, it attempts to write the data to the client computer. The result of a flush() invocation can be one of three possibilities:

  • If the user denies LSO storage for the domain, or if the Flash Player fails to save the data for some reason, the data is not saved and the method throws an Error.

  • If the amount of disk space required to save the LSO’s data is less than the local storage setting for the domain, the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required