3 Replies Latest reply on Nov 6, 2009 2:33 AM by Bertrand G.

    chaining of eventListeners BUGS with Loader

    Bertrand G.

      My goal is to load numerous images to be displayed in an AIR application. For this I use the following code. The counter counts the number of times each event is triggered. The problem is that loading 1024 images:

      -  in the onLoadingComplete function the counter finishes at 1024... this is fine

      -  in the onParsingComplete function the counter finishes at various numbers (412, 405, 380...) different at each run

      - nothing goes wrong with less than around 500 images, all are loaded

       

      My assumptions:

      - 'complete' event not triggered if image content is the same (I am using 8 copies of 128 different images)

      - loaders have a concurrency problem

      - eventListeners asynchronous behavior is buggy

       

      ANY IDEAS, FEEDBACK ? Should I file a bug ? (by the way I am using Flex 4 on a mac)

      for each( var file: File in files ) {
           if ( ! file.isDirectory && file.extension == "jpeg" || file.extension == "jpg" || file.extension == "png" ) {
           var urlLoader: URLLoader = new URLLoader( );
           urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
           urlLoader.addEventListener(Event.COMPLETE, onLoadingComplete );
              urlLoader.load( new URLRequest(file.url) );
           }
      }
      private function onLoadingComplete( e: Event ):void
      {
           var dispLoader: Loader = new Loader( );
           dispLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, onParsingComplete);
           dispLoader.loadBytes( e.target.data );
           //counter++ --> here end up with counter = 1024
      }
      private function onParsingComplete( e: Event ):void
      {
           var img: BitmapImage = new BitmapImage( );
           img.source = (e.target.content as Bitmap).bitmapData;
           _images.push( img );
           counter++; --> here end up with counter = 412, 405, 512 or in debug mode around 380
      }
      

        • 1. UPDATE
          Bertrand G. Level 1

          I've been doing a batchLoader loading batch of 100 images every 2sec... and it works; so it seems that when there is too many eventListeners running, they get ignored. I will need to do a more robust workaround for this, but...

           

          Here is a code snipplet:

          private var files:Array;
          private var offset:int = 0;
          private const BATCH:int = 100;
          
          [...]
          var timer:Timer = new Timer(2000, Math.ceil(files.length/BATCH));
          timer.addEventListener(TimerEvent.TIMER, batchLoad);
          timer.start();
          
          [...]
          
          private function batchLoad(e:TimerEvent):void {
                    var file:File;
                    var urlLoader: URLLoader;
                    for (var i:int=offset; i<offset+BATCH && i<files.length; i++)  {
          //                    for each( var file: File in files ) {
                    file = files[i]; 
                    if ( ! file.isDirectory && file.extension == "jpeg" || file.extension == "jpg" || file.extension == "png" ) {
                         urlLoader = new URLLoader( );
                         urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
                         urlLoader.addEventListener(Event.COMPLETE, onLoadingComplete );
                         urlLoader.load( new URLRequest(file.url) );
                         counter++;
                    }
               }
               trace ("counter: "+counter);
               offset += BATCH;
          }
          

          • 2. Re: UPDATE
            paul.williams Level 4

            Why not just load them one at a time? Is it too slow?

            • 3. Re: UPDATE
              Bertrand G. Level 1

              well using this method, the load is done asynchronously, so I needed to pace the the loading to get it working properly. The other method I used to load images were a nightmare when I want to unload the images and recover the memory... See Anton_AL thread about this: Serious memory issues

               

              Now I will need to do a batch loader that will work with the number of files loaded instead of a poor timer.

               

              But anyway that means that the eventListeners are not thread safe... which I have a tendency to consider as a bug...