0

I'm using a local shared object as means to transfer data between two swf's running at the same time.

so = getLocal("mySO");

// set the value from a user input, and set it to 'connected'
so.setProperty(txtName.text, "connected")

// every few seconds check if the other SWF started and modified value
onTimer 
{
 var data:Object = so.data
// breakpoint and inspect the data with debugger
}

The last commented line is where the problem is. In debugger, no changes to data are detected, but when inspecting .sol file with .minerva, I see the changes from both SWF's. So, although shared object is modified by SWF 2, the SWF 1 does not see these changes. Is this how's it supposed to be?

ps: I know I could use LocalConnection for communicating between two running SWF's but still want to know about the SO if there's some kind of a limitation.

UPDATE:

Run the compiled Flex app from disk twice, put the "swf2" into textfield of the second one. Press start on both. The first one that is started never detects that the second one was connected.

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
               minWidth="955" 
               minHeight="600"
               creationComplete="application1_creationCompleteHandler(event)"
               >

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            private var timer:Timer;
            private var so:SharedObject;

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                so = SharedObject.getLocal("mySO");

                timer = new Timer(1000);
                timer.addEventListener(TimerEvent.TIMER, onTimer);

            }

            protected function button1_clickHandler(event:MouseEvent):void
            {
                so.setProperty(txtSwf.text, 'connected');
                so.flush();
                timer.start();

            }


            protected function onTimer(event:TimerEvent):void
            {
                so = SharedObject.getLocal("mySO");
                txtLog.text += so.data["swf1"] + "\n";
                txtLog.text += so.data["swf2"] + "\n";
                txtLog.text += "------------ \n";

            }

        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:TextInput id="txtSwf" x="10" y="50" text="swf1"/>
    <s:Button x="170" y="50" label="Start" click="button1_clickHandler(event)"/>
    <s:TextArea id="txtLog" x="10" y="154" width="279" height="112" />


</s:Application>
Ska
  • 6,658
  • 14
  • 53
  • 74
  • 1
    LocalConnection is the better route for you. Aside from that are the SWFs in the same domain? – The_asMan Jun 17 '12 at 22:13
  • SWF's are ran from IDE and one from disk with FP, no browser. They DO both write to SO, I verified that. They don't detect the changes others' have made though. As I said, LC is another option but I'd still like to learn about SO why is it behaving like this. – Ska Jun 17 '12 at 23:27
  • I believe running one from disk and one from IDE is not the same domain. You have to run them all from the disk or all from the browser. Try publishing everything to the web and see if you get the results you are looking for that way. – The_asMan Jun 18 '12 at 17:35
  • @The_asMan I tried running both from disk too. – Ska Jun 18 '12 at 19:59
  • Well try running both in a browser in the same domain. And by in a browser I mean on a server that has a domain or at least an IP address. Running in a browser off the hard drive is not what I am talking about. Also post your embed code. – The_asMan Jun 19 '12 at 00:38
  • Nope, it doesn't work, I ran them from localhost. Again, the inspection of the SO shows that they are both writing to it. It just that they don't detect the changes the other swf's made during runtime. They *do* pick up changes if the SWF runs again. – Ska Jun 19 '12 at 02:22
  • I think I found my solution, check my answer. – Ska Jun 19 '12 at 02:49

2 Answers2

0

You need to force the reading of the file, but not of the data which client already has :/

so = getLocal("mySO");

// set the value from a user input, and set it to 'connected'
so.setProperty(txtName.text, "connected")

// every few seconds check if the other SWF started and modified value
onTimer 
{
    so = getLocal("mySO"); // read the so file.

    var data:Object = so.data
}
Jevgenij Dmitrijev
  • 2,228
  • 1
  • 15
  • 23
0

Phew, found an answer. Jevgeenij gave me a lead to forcing the re-read of the shared object, but his code didn't work exactly. what I needed to add was this.

so = getLocal("mySO");

// set the value from a user input, and set it to 'connected'
so.setProperty(txtName.text, "connected")

// every few seconds check if the other SWF started and modified value
onTimer 
{
    so = getLocal("mySO"); // read the so file.

    var data:Object = so.data;

    so = null // MAKE IT NULL AND HOPE IT'S GC'D BEFORE NEXT TIMER
}

The trick was to set it to null after use, only then it is forcefully re-read and values written from other SWF instance picked up.

Ska
  • 6,658
  • 14
  • 53
  • 74