loader - unloading failing?

tried everything! want to stop a loader half way through loading (to cancel and load something else).

I use one loader to load hundreds of jpg's, when one is no longer needed and hasn't finished loading yet, I need to stop downloading it, and start downloading the next I have queued.

It said in 'livedocs.adobe.com' that:
-"issuing a subsequent load() request terminates the previous request, if still pending, and commences a new load. "
-"Use the unload() method to remove movies or images loaded with this method, or to cancel a load operation that is in progress."

however when I use either it doesn't free up the loader, and it still loads it!

how can I terminate it in mid-flow?
RoryRAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

blue-genieCommented:
hi i just tested it out to see, and you're right it doesn't change is its busy. that is rather silly.

i'm thinking out loud here but as a workaround for you, maybe use 2 loaders and use alternate ones since you are loading hundreds of jpegs, if you don't want it it's still not added to stage so you won't see it anyway.
0
RoryRAuthor Commented:
thanks,

it doesn't need solving to allow more loading, as when it has finished loading the no longer needed one it can carry on. What I want is it not to waste the bandwidth downloading something that is no longer needed, if I download something else along side it will be slower, need the script to be very efficient.

basically my script is a google maps style script, loading many images in a grid and zooming in to different resolution images, so if you scroll or zoom past an image and then its still loading when it is not in focus then it is slowing down the whole thing.
0
IqAndreasCommented:
What do you mean by "free up the loader".

NEVER EVER use the same Loader instance for two different loadings.

Instead, remove all references to the old loader (by setting everything that accesses it to null and removing it from the stage) and replace it with a new loader, like the code attached.

Do you understand, or should I explain anything better? Here is a bit further reading on the matter:
http://www.experts-exchange.com/Software/Photos_Graphics/Web_Graphics/Macromedia_Flash/ActionScript/Q_26080704.html?cid=1131#a32193524

If you provide the code you are using, I can modify it to make this system work.

Good luck with your programming,
Andreas
//Let's say this is the code you are using to create the loader:
var myLoader:Loader = new Loader()
addChild(myLoader);
var url:URLRequest = new URLRequest("myimage.jpg"); 
myLoader.load(url);

//This is the code you would use to remove it from memory
removeChild(myLoader);
myLoader.stopAndUnload(true); //Always a good idea, but not REQUIRED to make it disappear
myLoader = null;

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

IqAndreasCommented:
Just as a tip, you might actually want to keep a reference to the image you already loaded in memory at times.

For instance, if an image no longer becomes necessary because the user has zoomed in so much so the next "layer" is loaded, chances are they will zoom back out at one point or another, and if you have a copy of that old image saved, it will make loading that image instant. :) That will also make it easier on the server so it doesn't have to send out as many images.

Do you understand?


What are you using to check if the image loading is still using bandwidth?

Chances are, unless you users are on dial-up, loading 2 images at once (the old one which didn't completely finish it's downloading, plus the new one) the user won't really notice. Most people have a good speed of internet anyway.

Good luck with your programming,
Andreas
0
RoryRAuthor Commented:
"NEVER EVER use the same Loader instance for two different loadings"
-why? maybe that's the problem then. will re-write my code and see if it fixes the problem.

"you might actually want to keep a reference to the image you already loaded in memory at times."
-I keep a certain amount, then I cull the oldest, it is a of 1 million jpgs on the bottom layer of zoom, there is a reason I unload! :)

"What are you using to check if the image loading is still using bandwidth?"
-implied, as I have lots of trace statements tracking it, so I can tell the images I cancel the loading are still loading.

"Most people have a good speed of internet anyway."
-the plan is for a multi-user application, I can't be wasteful of bandwidth, my server will suffer. there is a lot of gain in optimising loading for speed of app and load on server.

0
IqAndreasCommented:
I'm glad you are in full control of this project and know what you are doing, rather than someone who asks the same question and says "I want my images to stop loading what they are loading and start loading something else just because I don't need the old thing anymore. And still loading the old thing is probably bad." I help out quite a bit on the Kirupa forums and get that quite a bit. They really have no idea why they don't want other items to stop loading, they just do, when there really is no reason for them to need to worry.

However, in this case, yes, you do need to stop Loaders from keep loading.


-I keep a certain amount, then I cull the oldest, it is a of 1 million jpgs on the bottom layer of zoom, there is a reason I unload! :)
Haha... yeah, you definitely will want to cull old images. :P


By using the same loader instance, I mean something like (in the example above) using the same "myLoader" to load something else.

However, there is a difference between using the same variable name and using the same Loader instance. This will create a NEW Loader, even though it uses the same variable name. If you only included the "myLoader.load()" part and used the same loader as you created before, that would be using the same loader instance (which again is bad)

Do you understand?
0
IqAndreasCommented:
Forgot the code...

Anyway, another thought came to mind. You are monitoring if it is still loading using the ProgressEvent, correct?

Are you sure that Flash keeps loading the ENTIRE image after the unload command is sent?

For instance, let's say you start loading an image. Every time bytes arrive, the listener for the progress event is run. Now, if you cancel the loading in mid-download, requests for new image "pieces" will stop. However, you still had some requests out there which are returning values to you.

This means that after you "unload()" the image, old pieces of the image might still be coming in for a second or so, causing the progress listener to run. However, since the image is unloaded, it is no longer asking for new image "pieces".

Do you think that could be the issue?
//This is the way you create the loader initially
var myLoader:Loader = new Loader()
addChild(myLoader);
var url:URLRequest = new URLRequest("myimage.jpg"); 
myLoader.load(url);

//BAD IDEA
myLoader.stopAndUnload(true);
myLoader.load(newUrl);

//GOOD IDEA
myLoader.stopAndUnload(true);
myLoader = new Loader(); //Here you replace it
myLoader.load(newUrl);

Open in new window

0
RoryRAuthor Commented:
"You are monitoring if it is still loading using the ProgressEvent, correct?"
-no, only have a function for complete, or error. I am checking using the bandwidth profiler and tracing.
"NEVER EVER use the same Loader instance for two different loadings"
I have re-written my code to use one variable to store the loader, but unload it and null it like you said, then create a new loader, my program is still functioning, the only change is now it loads one image at a time until you try to cancel, then a new loader starts and the old loader continues until it is finished (so it does it one by one until a load is canceled then its downloading 2 at once until the canceled one is finished then it goes back to one at a time)
"old pieces of the image might still be coming in for a second or so .... Do you think that could be the issue?"
- I thought it might, but it doesn't appear to be that. When I watch with the bandwidth profiler they all continue until completed. all that changes is it downloads 2 at once.

-do you need to add and remove the event listeners each time?
here is a bit of code to show you how I have it set up - I call the unload function to cancel (tried it with various ways):

function load_image(location:String){
    trace("loading " + currently_loading);
    if (imageloader!=null) unload_loader();
    imageloader=new Loader();
    imageloader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded_image,false,0,true);
    imageloader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, image_failed,false,0,true);
    imageloader.load(new URLRequest(location));
}
function unload_loader() {
    imageloader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaded_image,false);
    imageloader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, image_failed,false);
    imageloader.unloadAndStop();
    imageloader = null;
}
0
RoryRAuthor Commented:
it often loads 2-4 images just by passing a layer / level of zoom, and if you are on a bad connection you can get 2 levels down by zooming quickly before they have a chance to load, as it is adding the images to a loading queue that isn't too much of an issue, as it only ends up trying to cancel 1 image loading (not 4-8) and it checks to see if it is still in focus before starting loading a new image (so it drops those 4-8 images from the queue)

but with the new 1 loader per item idea, 3+ images start loading concurrently when I quickly zoom down many zoom levels (as when it cancels and then starts loading the next in a queue it is still loading the canceled ones). All the happens is cancel is letting my code start a new loader, not really canceling the loading!
0
RoryRAuthor Commented:
Just found this:

Isn't the close() - method in the Loader supposed to cancel the load?

I still recieve progress events after calling it until it's completely loaded.
Use the unload() method to remove movies or images loaded with this method, or to cancel a load operation that is in progress.I've understood that this problem occurs only in the Flash IDE.Still very annoying when developing.I do unload as well but the stream and objects keeps loading.

I will have to test this. but not sure how to test this?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IqAndreasCommented:
You should just be able to add this to your "unload_loader" function:
imageLoader.close();

Or do you mean that you are not sure how to test if images are still downloading or not?

Try listening for the Progress Event and detect if it is still firing even after you close the connection. I feel silly for not looking deeper into the language reference for that function.

In case it is any use, here is the page with all the Loader's functions on it in case there is anything else that is useful in there:
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/display/Loader.html
0
RoryRAuthor Commented:
sorry, that was 3 comments off a forum I put together badly, saying that in the IDE it doesn't unload even if you do it correctly.

"Or do you mean that you are not sure how to test if images are still downloading or not?"
-yeah, when not in the IDE (eg in browser it is supposed to work right). I will try writing a test function.
0
RoryRAuthor Commented:
ok, I have just tried to test it one my website, it's hard to tell, but I think it is working :-S. I used a 7 level canvas so that it would pile up 6+ redundant loads if it wasn't working and it seemed to skip straight to the last when I zoomed straight in... but I am not sure.

I am hoping it is correct that it works fine in the flash player, annoyingly it doesn't have simulate download or the bandwidth profiler to check whats happening. Adobe as usual has annoyed me with bugs! the IDE should function the same. cruddy software, as usual.

I am still trying to find a good way to verify if it is functioning correctly, may just use a packet sniffer. then I am sure!
0
IqAndreasCommented:
I'm not sure if there is anything similar for Windows, but in Linux I have a handy dandy network monitor which displays a line chart of the current bandwidth consumption.
http://mmeiser.com/blog/images/Alpha4.png

DI forget, but I'm thinking there should be if you bring up the dialog from CTRL+ALT+DEL. Darn it, I have been in Linux so long I forgot what that dialog is called... :P
0
blue-genieCommented:
"NEVER EVER use the same Loader instance for two different loadings." that's interesting

i did a test recently a couple of months back with a file that was chugging up the system when left for a couple of hours.
i was using new instances each time.
i stopped doing that and reused the same one and that saved about 20% CPU chugg.

just me experience.
0
IqAndreasCommented:
As long as you remove all references to your loaders and thereby freeing them up for garbage collection, it shouldn't be too noticeable of a cpu tax. But I could be wrong. I personally haven't done detailed tests on the matter myself.

It is possible to use the same loader twice, but it's easier for beginners (as well as seasoned developers keeping track of so much in their project) if each loader instance is associated with one URL, at least in my opinion. It also makes debugging easier.

There is a slight overhead in creating new objects rather than reusing old ones, however, unless you are creating several new Loader objects per frame, the "time gobbling" is minimal.


Anyway, I hope everything works out for you Rory. Just reply back if you are still having issues.

Good luck with your programming,
Andreas
0
blue-genieCommented:
Iq, bear in mind that the AVMs GC is shoddy at best.
0
RoryRAuthor Commented:
just checking it all, been busy.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.