Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7431297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:16:26+00:00 2026-05-29T09:16:26+00:00

Okay, here is one weird bug… In my app, I load multiple xml files.

  • 0

Okay, here is one weird bug…

In my app, I load multiple xml files. And to load those, I always create a new URLLoader. Nothing special.

The first file works fine, the second one does, too.
The third one, however, reports to have loaded fine, but the data it holds is actually the data of the second file.
The current workaround is to load the third file twice, which somehow works… weird.

I have absolutely no clue how such a thing can happen.
All three files are of course different files with different paths.

Here is the relevant class, hope it is of any use, if something is unclear, please ask.
The DownloadJob class is merely a helper class that holds a String and a Function object. The latter one is called when the download is finished.

    // Actual class stuff
    private var _downloadQueue  :Array = new Array();

    /**
     * Adds a download to the queue. Will be started immediatly.
     * @param   p_url       The URL of the download.
     * @param   p_callback  The function to call when the download is finished. Has to take a DisplayObject/Sound/String as first parameter.
     */
    public function addDownload(p_url:String, p_callback:Function) :void
    {
        var job :DownloadJob = new DownloadJob(p_url, p_callback);
        _downloadQueue.push(job);

        debug.ttrace("added: " + job.url);

        // If it is the only item, start downloading
        if (_downloadQueue.length == 1)
        {
            var job :DownloadJob = DownloadJob(_downloadQueue[0]);
            load(job);
        }
    }

    /**
     * Will call the callback and dispatch an event if all loading is done.
     * @param   p_event The event that is passed when a download was completed.
     */
    private function downloadComplete(p_event:Event) :void 
    {
        var job :DownloadJob = DownloadJob(_downloadQueue[0]);
        var downloaded :Object = p_event.target;
        _downloadQueue.splice(0, 1);

        debug.ttrace("completed: " + job.url);

        // Notify via callback
        if (downloaded is LoaderInfo)
        {
            job.callback(downloaded.content);
        }
        else if (downloaded is Sound)
        {
            job.callback(downloaded);
        }
        else if (downloaded is URLLoader)
        {
            // This one holds the data of the previously loaded xml, somehow
            job.callback(URLLoader(downloaded).data);
        }

        // Continue downloading if anything is left in the queue
        if (_downloadQueue.length > 0)
        {
            var job :DownloadJob = DownloadJob(_downloadQueue[0]);
            load(job);
        }
        else
        {
            dispatchEvent(new Event(EVENT_DOWNLOADS_FINISHED));
        }
    }

    /**
     * Will load the passed job immediatly.
     * @param   p_job   The job to load.
     */
    private function load(p_job:DownloadJob) :void
    {
        // Different loaders needed for data, sound and non-sound
        if (p_job.url.indexOf(".xml") != -1 ||
            p_job.url.indexOf(".txt") != -1)
        {
            var urlloader :URLLoader = new URLLoader();
            urlloader.addEventListener(Event.COMPLETE, downloadComplete);
            urlloader.addEventListener(IOErrorEvent.IO_ERROR, handleError);
            urlloader.load(new URLRequest(p_job.url));
        }
        else if (   p_job.url.indexOf(".mp3") != -1 &&
                    p_job.url.indexOf(".flv") != -1)
        {
            var sound :Sound = new Sound();
            sound.addEventListener(Event.COMPLETE, downloadComplete);
            sound.addEventListener(IOErrorEvent.IO_ERROR, handleError);
            sound.load(new URLRequest(p_job.url));
        }
        else
        {
            var loader :Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, downloadComplete);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleError);
            loader.load(new URLRequest(p_job.url));
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:16:27+00:00Added an answer on May 29, 2026 at 9:16 am

    I spent some time on your class and could not find any errors, even when dispatching random addDownload commands from a timer – everything went as expected, no mix-ups, no weird data residue. I can only guess that perhaps the issue has something to do with weird variable handling in ActionScript, if it really is within the code you posted at all.

    So then I took the liberty of rearranging your Class a bit:

    • I changed the queue’s type to Vector.<DownloadJob>. This lets us get rid of all the type casting.
    • The current download is stored in the field variable currentJob. There will only be one job to work on at a time, anyway. This eliminates all the function arguments.
    • A job will only be added to the queue, if currentJob isn’t null, i.e. a download is actually in progress. We don’t have to queue things, if there’s no need to wait. This leaves only a single call to each push() and splice(). No more uncertainties in respect to when things get added and removed.
    • Removed hungarian notation (we don’t do that in ActionScript).
    • Split your larger methods into smaller, more readable chunks (helps me think 🙂 ).
    • Removed all the comments, except for the API method (the code should speak for itself, I believe).

    Try to exchange the code below with your previous class and see if the problem still exists. If it does, I am pretty sure the problem exists outside of your loader, possibly in the callback functions that process the payload.

    private static const EVENT_DOWNLOADS_FINISHED : String = "EVENT_DOWNLOADS_FINISHED";
    private var currentJob : DownloadJob;
    private var downloadQueue : Vector.<DownloadJob> = new Vector.<DownloadJob> ();
    
    /**
     * Adds a download to the queue. Will be started immediatly.
     * @param   url       The URL of the download.
     * @param   callback  The function to call when the download is finished. Has to take a DisplayObject/Sound/String as first parameter.
     */
     public function addDownload ( url : String, callback : Function ) : void {
        var job : DownloadJob = new DownloadJob ( url, callback );
        if (currentJob) downloadQueue.push ( job );
        else {
            currentJob = job;
            load ();
        }
    }
    
    private function load () : void {
        if ( jobIsText () ) loadText ();
        else if ( jobIsSound () ) loadSound ();
    }
    
    private function jobIsText () : Boolean {
        var url : String = currentJob.url;
        return url.indexOf ( ".xml" ) != -1 || url.indexOf ( ".txt" ) != -1;
    }
    
    private function jobIsSound () : Boolean {
        var url : String = currentJob.url;
        return url.indexOf ( ".mp3" ) != -1;
    }
    
    private function loadText () : void {
        var urlloader : URLLoader = new URLLoader ();
        urlloader.addEventListener ( Event.COMPLETE, handleComplete );
        urlloader.addEventListener ( IOErrorEvent.IO_ERROR, handleError );
        urlloader.load ( new URLRequest ( currentJob.url ) );
    }
    
    private function loadSound () : void {
        var sound : Sound = new Sound ();
        sound.addEventListener ( Event.COMPLETE, handleComplete );
        sound.addEventListener ( IOErrorEvent.IO_ERROR, handleError );
        sound.load ( new URLRequest ( currentJob.url ) );
    }
    
    private function handleComplete ( ev : Event ) : void {
        processPayload ( ev.target );
    
        if (downloadQueue.length > 0) loadNext ();
        else dispatchEvent ( new Event ( EVENT_DOWNLOADS_FINISHED ) );
    }
    
    private function handleError ( ev : Event ) : void {
        trace ( "Error while downloading:" + currentJob.url );
    }
    
    private function processPayload ( loader : Object ) : void {
        currentJob.callback ( getPayload ( loader ) );
        currentJob = null;
    }
    
    private function loadNext () : void {
        currentJob = downloadQueue.splice ( 0, 1 )[0];
        load ();
    }
    
    private function getPayload ( loader : Object ) : Object {
        return (loader is LoaderInfo) ? loader.content :     
                   (loader is URLLoader) ? URLLoader ( loader ).data :
                       loader;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, Here's a breakdown of what's up: <? $foo = new Imagick(); ?> works
Okay, this one seems to me a bit weird. Any help would be greatly
Okay, here's one for the pro's: For a couple of years now, i've been
Okay, here's my situation. I have a WPF app that I have created that
Okay, I'm going to sound like an idiot with this one. Here goes. I've
Okay, here's what I have... On one server, a WCF hosted in IIS. This
Okay, here a situation: 1) I have a Panel called panel1 that consist one
Okay. So here's my question: I am making a data parser in Clojure. One
Okay, two admissions here, one is I'm sure this question has been answered and
Okay, redoing the question to a much simpler one. Here's my HTML code. <p>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.