DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 16.7 Saving Remote Shared Object Data

16.7.1 Problem

You want to save the data stored in a remote shared object.

16.7.2 Solution

Define an onAppStop( ) callback handler or use the SharedObject.flush( ) method on the server.

16.7.3 Discussion

When you are working with RSOs, there are really two shared objects: the object as it exists on the server and the object as it exists on the client. And in a scenario in which the shared objects are persistent, there is both a persistent, disk-based version and a working version stored in memory. As the client makes changes to the data in the client-side, memory-based shared object, it is automatically sent to the server, and the data in the server-side, memory-based shared object is synchronized with the client-side data. And once the server data is updated, the onSync( ) method is invoked for all clients connected to the RSO.

// Create or open an RSO and connect to it.
my_r_so = SharedObject.getRemote("myFirstRSO", myConnection.uri);
my_r_so.connect(myConnection);

// Add data to the RSO. The data is automatically sent to the server.
my_r_so.data.myData = "Your Value Here.";

However, even though the memory-based shared objects are kept in synch with one another, the persistent, disk-based shared objects are not always kept in synch with the current data. This is because file access is a relatively "expensive" task. It requires more time to read and write to disk than to read and write to memory. As such, the default behavior for shared objects is that the data is not written to disk until the memory-based object is about to be deleted. This occurs on the client when the Flash movie is being closed, and it occurs on the server when the application is being shut down. You can read more about how to save the client-side data to disk in Recipe 16.4. For more information on storing server-side data to disk, keep reading.

The RSO data should need to be written to disk only if and when the FlashCom application stops. To tell FlashCom server to write the RSO data to disk when the application stops, define an application.onAppStop( ) method that returns true in your main.asc file on the server, as follows:

application.onAppStop = function (  ) {
  return true;
};

You can also invoke the flush( ) method on the server-side shared object (from the .asc file) at any point to explicitly write the data to disk:

// Place this in your main.asc file or other included .asc file.
ssSo.flush(  );

16.7.4 See Also

Recipe 16.6

    [ Team LiB ] Previous Section Next Section