Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Bug? Strange results when using multiple UrlLoader instances simultaneously — Gideros Forum

Bug? Strange results when using multiple UrlLoader instances simultaneously

ndossndoss Guru
edited May 2012 in Bugs and issues
When I have two simultaneously active UrlLoaders, I get some weird results. In the code below, the callback function registered with the first call to my "load" function never seems to get called. The callback registered with the second call to my load function seems to get called for all url loader events, even though I've created multiple url loaders?

If I sequentialize the two calls, I don't have this problem.
function load(url, callback)
   local urlLoader = UrlLoader.new(url, UrlLoader.GET)
 
   function handleResponse(response, error)
      response.url     = url
      response.error   = error
      if callback then callback(response) end
   end
 
   urlLoader:addEventListener(Event.COMPLETE, function(response) handleResponse(response, false) end)
   urlLoader:addEventListener(Event.ERROR,    function(response) handleResponse(response, true) end)
end
 
load("<a href="http://google.com"" rel="nofollow">http://google.com"</a>, function(response)
   print("IN FIRST CALL FOR GOOGLE")
   if response.error then
      print("ERROR: loading google")
   else
      print("--------------- Loaded google ("..response.url..")")
      print(response.data)      
   end
end)
 
load("<a href="http://apple.com"" rel="nofollow">http://apple.com"</a>, function(response)
   print("IN SECOND CALL TO GOOGLE")
   if response.error then
      print("ERROR: loading apple")
   else
      print("--------------- Loaded apple ("..response.url..")")
      print(response.data)      
   end
end)
Results from gideros desktop player (wine running under linux):
IN SECOND CALL TO APPLE
--------------- Loaded apple (<a href="http://apple.com" rel="nofollow">http://apple.com</a>)
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="<a href="http://www.google.com/">here</A>" rel="nofollow">http://www.google.com/">here</A></a>.
</BODY></HTML>
 
IN SECOND CALL TO APPLE
--------------- Loaded apple (<a href="http://apple.com" rel="nofollow">http://apple.com</a>)
<head><body> This object may be found <a HREF="<a href="http://www.apple.com/">here</a&gt" rel="nofollow">http://www.apple.com/">here</a&gt</a>; </body>

Comments

  • At first sight of your code,it seems UrlLoader is a singleton in gideros internal.
    But I try the following simple code,it works well,so UrlLoaders support simultaneous call.

    Maybe there is a little problem with your code, I'm a newbie in lua, I can not tell what exactly it is, but It seems your callback argument in load does not associate with the urlloader object.
    local loader1 = UrlLoader.new("<a href="http://google.com&quot" rel="nofollow">http://google.com&quot</a><img class="emoji" src="https://forum.giderosmobile.com/resources/emoji/wink.png" title=";)" alt=";)" height="20" />
     
    local loader2 = UrlLoader.new("<a href="http://apple.com&quot" rel="nofollow">http://apple.com&quot</a><img class="emoji" src="https://forum.giderosmobile.com/resources/emoji/wink.png" title=";)" alt=";)" height="20" />
     
    local function onComplete1(event)
       print("google")
    end
     
    local function onComplete2(event)
        print("apple")
    end
     
    loader1:addEventListener(Event.COMPLETE, onComplete1)
    loader2:addEventListener(Event.COMPLETE, onComplete2)
  • Maybe you can pass callback as the first argument to the listener function,
    This code works as expected.
    function load(url, callback)
       local urlLoader = UrlLoader.new(url, UrlLoader.GET)
     
       function handleResponse(mycallback ,response, error)
          response.url     = url
          response.error   = error
          if mycallback then mycallback(response) end
       end
     
       urlLoader:addEventListener(Event.COMPLETE, function(mycallback, response) handleResponse(mycallback, response, false) end, callback)
       urlLoader:addEventListener(Event.ERROR,    function(mycallback, response) handleResponse(mycallback, response, true) end, callback)
    end
     
    load("<a href="http://google.com"" rel="nofollow">http://google.com"</a>, function(response)
       print("IN FIRST CALL FOR GOOGLE")
       if response.error then
          print("ERROR: loading google")
       else
          print("--------------- Loaded google ("..response.url..")")
          print(response.data)      
       end
    end)
     
    load("<a href="http://apple.com"" rel="nofollow">http://apple.com"</a>, function(response)
       print("IN SECOND CALL TO APPLE")
       if response.error then
          print("ERROR: loading apple")
       else
          print("--------------- Loaded apple ("..response.url..")")
          print(response.data)      
       end
    end)
  • Thanks @alexzheng! This works, but I'm still not sure I understand why ... or how you came up with this. It's late so I hope I'll understand your brilliance after I get some sleep :). Thanks again!
  • Now I understand the problem, I didn't make handleResponse local. Thanks again @alexzheng
  • alexzhengalexzheng Guru
    edited May 2012
    Yes, it's clear now. Thank you also.
    It seems just the same as in C/C++ using a global or static Variable in a function that make the function not reentrantable.
Sign In or Register to comment.