Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
What is the correct way to use the UrlLoader class ? — Gideros Forum

What is the correct way to use the UrlLoader class ?

techdojotechdojo Guru
edited August 2012 in General questions
I'm trying to implement a download queue for a list of files.
Initially I create a connection using UrlLoader.new(url) and add the appropriate event listeners.
The first thing both the ERROR and COMPLETE listeners do is remove both listeners from the connection.

I then parse the downloaded file and then create a new connection (still from within the original callback) and re add the event listeners (my thinking here is that the listeners were associated with the PREVIOUS connection not THIS one), however once the original callback has finished my app appears to hang and the new connection handlers never get called.

Anyone ( @Atilim ) got any ideas?

@Scouser suggested creating a single connection without the URL and using the load() function - is this a better solution, anyone got any code they want to share?

Thanks (in advance)
WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill

Comments

  • It's looking very much like the problem might be down to calling the UrlLoader:load (or new) function from within an existing UrlLoader event callback.

    I'm going to try adding a simple enterFrame listener that will initialise the load calls via a simple finite state machine, I know I can make multiple calls to UrlLoader objects as I've done it elsewhere (just not processing a queue), so I'm going to try this and let you know how I get on.
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • Ok - well I've got something sorted at least - it's a bit of a hack, but it works!

    To download a single file you must create an instance of a UrlLoader and assign the appropriate event handlers to it.

    Once the download is complete you are free to work with the data - BUT you can't reuse the connection (ie make a subsequent :load() call), other than removing the event handlers you also CAN'T do anything with the instance of the connection (ie the original return value from UrlLoader:new()) - you certainly can't create a NEW connection and assign it to the variable (don't ask me why!) otherwise the any attempt to download will just cause it to crash.

    The way I got round the problem was to have a table of UrlLoader instances and create one per file.

    Initially the table was empty - and I used a onEnterFrame listener to watch the state of a flag that was set each time a download completed and was then used to trigger the next load from the onEnterFrame listener.

    Once all the files had been downloaded I then set a different flag which caused the onEnterFrame listener to pickup the fact that all the files had been downloaded and then nil out all the references to the UrlLoader instances before continuing.

    I'm not sure if this the best method to use but until I get official word from those "in the know" it'll do.

    :(
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • atilimatilim Maintainer
    @techdojo can you post an example where the app hangs?
  • I'll try and create one as soon as
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • Weird.
    I tried to create a trivial example to show off the problem and it ended up working ???
    I then went back to try and undo the changes and make my project not work and that's working as well... ( even more ???? )

    Must go and have a chat with the IT guy who manages the site and find out if he's messed with anything!

    Anyway if anyone's interested here's an example of downloading a list of files...
    -- ------------------------------------------------------------------------------------------
    -- Global settings files..
    -- ------------------------------------------------------------------------------------------
     
    local dlQueue = { 
    	"<a href="http://your" rel="nofollow">http://your</a> file here.xml",
    	"<a href="http://your" rel="nofollow">http://your</a> file here.xml",
    	"<a href="http://your" rel="nofollow">http://your</a> file here.xml",
    	"<a href="http://your" rel="nofollow">http://your</a> file here.xml",
    	"<a href="http://your" rel="nofollow">http://your</a> file here.xml",
    	"<a href="http://your" rel="nofollow">http://your</a> file here.xml",
    	"<a href="http://your" rel="nofollow">http://your</a> file here.xml",
    	"<a href="http://your" rel="nofollow">http://your</a> file here.xml",
    }
     
    local dlSize = #dlQueue
    local dlCount = 1
     
    -- ------------------------------------------------------------------------------------------
     
    local onDownloadComplete, onDownloadProgress, onDownloadError = nil,nil,nil
     
    -- ------------------------------------------------------------------------------------------
     
    local function removeDownloadHandlers(self)
     
    	self.connection:removeEventListener(Event.COMPLETE, onDownloadComplete, self)
    	self.connection:removeEventListener(Event.ERROR,    onDownloadError, self)
    	self.connection:removeEventListener(Event.PROGRESS, onDownloadProgress, self)
    end
     
    -- ------------------------------------------------------------------------------------------
     
    onDownloadComplete = function(self,event)
     
    	print("HERE... GOOD")
     
    	removeDownloadHandlers(self)
     
    	print("Downloaded "..dlQueue[dlCount].." "..string.len(event.data).." bytes")
     
     
            -- DO STUFF WITH event.data here!
     
     
    	-- Update the download queue...
     
    	dlCount = dlCount + 1
     
    	if dlCount > dlSize then 
    		print("Got to the end of the download queue - time to finish")
    	else
     
    		local ac = UrlLoader.new()
     
    		ac:addEventListener(Event.COMPLETE, onDownloadComplete, self)
    		ac:addEventListener(Event.ERROR,    onDownloadError, self)
    		ac:addEventListener(Event.PROGRESS, onDownloadProgress, self)
     
    		print("About to load "..dlQueue[dlCount])
     
    		ac:load(dlQueue[dlCount])
     
    		self.connection = ac
     
    	end
    end
     
    -- ------------------------------------------------------------------------------------------
     
    onDownloadProgress = function(self,event)
    	print("PROGRESS : "..event.bytesLoaded.." bytes")
    end
     
    -- ------------------------------------------------------------------------------------------
     
    onDownloadError = function(self,event)
     
    	print("HERE... BAD")
    	removeDownloadHandlers(self)
    	print("ERROR : Downloading "..downloadQueue[downloadCount])
     
    end
     
    -- ------------------------------------------------------------------------------------------
     
    Downloader = Core.class(Sprite)	 -- As this is a sprite an enterFrame handler can be added to work as a flash style pre-loader !
     
    function Downloader:init()
     
    	self.connection = nil
     
    	local ac = UrlLoader.new()
     
    	ac:addEventListener(Event.COMPLETE, onDownloadComplete, self)
    	ac:addEventListener(Event.ERROR,    onDownloadError, self)
    	ac:addEventListener(Event.PROGRESS, onDownloadProgress, self)
     
    	print("About to load "..dlQueue[dlCount])
     
    	ac:load(dlQueue[dlCount])
     
    	self.connection = ac
     
    end
     
    -- ------------------------------------------------------------------------------------------
     
    local download = Downloader.new()
     
    ------------------------------------------------
     
    -- End of file..
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • atilimatilim Maintainer
    Accepted Answer
    hahaha that's funny :) btw, I've found the bug about reusing the connection.
Sign In or Register to comment.