Skip to main content
January 13, 2012
Answered

URLLoader event timing

  • January 13, 2012
  • 2 replies
  • 949 views

I'm having an issue getting URLLoader to work the way I want. I basically need to pull information from xml files, parse that information into objects, and then work with said objects afterwards.

Here's pseudocode for my current implementation (which does not work)

   var xmlLoader:URLLoader = new URLLoader();

   xmlLoader.addEventListener(Event.COMPLETE, loadXML);

   xmlLoader.load(new URLRequest("xml1.xml"));

   xmlLoader.load(new URLRequest("xml2.xml"));

   function loadXML(e:Event): void {

          process xml data into objects

   }

   do stuff with objects

When the debugger runs, the do stuff with objects portion runs first. Is there a way for me to make this work without spaghettifying my code?

This topic has been closed for replies.
Correct answer Ned Murphy

The timing problem is easy... the code doesn't run in a serial manner waiting for each line before it to process.  Once the Loader is loading, the processing moves on to the next executable line, which is your "do Stuff" if it is not contained somehow (as in having it in a function that gets called when things are ready)  So you need to initiate "do stuff" within the loadXML function if doing stuff depends on the XML being loaded first.

2 replies

January 14, 2012

Thank you, that's what I was considering doing, but it makes my code a lot more ugly, especially since I rely on both xml files for my "do stuff" section. I think I have a solution, but its spaghetti.

It's incredibly restrictive if there's no way around the rule that all events trigger after the current function finishes.

Ned Murphy
Legend
January 14, 2012

I don't follow your logic regarding "incredibly restrictive".  An event triggers when the event has occured, not when a function finishes.  So a COMPLETE event is triggered when the file COMPLETEs loading.  If you have to wait until the xml files are loaded before you can do stuff, then that is the only thing restricting you, and that is what anyone faces in that situation - in any programming language.   Did you expect it to trigger before the file was loaded?  What would you do with a file that hasn't loaded yet?

January 14, 2012

I didn't really mean to compare it to other languages. Perhaps its a concern that exists in other ones as well. It just seems really messy to have to code like this:

loaderFunction1(){

     load stuff

     if (all function1 files loaded)

          loaderFunction2();

loaderFunction2(){ //dependant on loaderFunction1's data

     load stuff     

     if(all function2 files loaded)

          loaderFunction3();

...

loaderFunctionN(){ //dependant on loaderFunctionN-1's data

     load stuff

     if(all functionN's files loaded)

          beginProgram()

beginProgram(){}

If there's a better way, please let me know.

Ned Murphy
Legend
January 13, 2012

A Loader can only process/hold one loading at a time.

January 13, 2012

Thank you Ned. I would have run into that problem after I solved the one I was pointing out above, so I appreciate it. That's easily solved by instantiating two loaders.

Still looking for an answer on the more important timing issue.

Ned Murphy
Ned MurphyCorrect answer
Legend
January 14, 2012

The timing problem is easy... the code doesn't run in a serial manner waiting for each line before it to process.  Once the Loader is loading, the processing moves on to the next executable line, which is your "do Stuff" if it is not contained somehow (as in having it in a function that gets called when things are ready)  So you need to initiate "do stuff" within the loadXML function if doing stuff depends on the XML being loaded first.