Friday, January 18, 2013

Shared Objects in multiple Applications

So, I had this problem of opening one Flex Application from another and passing on some data to it entirely in the client side. What hit was using SharedObject to pass around the data. The one roadblock though was that Application2 was not able to read the SharedObject created by Application1. A bit of research led to my understanding as below.

In the normal sense, when we create a SharedObject we write the code as below,
var so:SharedObject = SharedObject.getLocal( "mySO" );

What the above line does is that it reads the SharedObject named "mySO" from the file "mySO.sol" placed in the local system. If the file does not exist, then it creates a new one with the same name.

In Windows 7, you can find the file in the following file path (This may vary based on the OS)
C:\Users\Pramod\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\TC6AK52F\localhost\work\myApp\flexbin\Application1.swf\mySO.sol

The problem what happens when Application2 tries to access mySO is that, it actually is looking into another folder to get the SharedObject and not the above one. So the path in which Application2 tries to access is (Again, this will vary based on the OS)
C:\Users\Pramod\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\TC6AK52F\localhost\work\myApp\flexbin\Application2.swf\mySO.sol

To get around this, what we can do is create a SharedObject in a common location that both the applications can access. We do this by writing code as below,
var so:SharedObject = SharedObject.getLocal( "mySO", "/" );

The above code creates/reads a SharedObject from the following location
C:\Users\Pramod\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\TC6AK52F\localhost

So now, both the applications can read the SharedObject. Hope this helps.

No comments :