network.request: No way to pass information to my networkListener function?

Hello,

I am trying to use network.request() to do an asynchronous https POST operation.

It works, but I need to send information to the networkListener about which request it is processing.  I gather that this is not possible with the current version of Corona SDK?

I not, then I would like to suggest this feature.  I think the best way to do this would be some way to send network.request() a delegate table.  When the request finishes, the system calls the “networkListener” function on the delegate table, or something.

-Ed

UPDATE: I just thought of a workaround, but it’s a bit ugly.

I will use the return value of network.request() as the key in a global table that maps to the “target object” that will absorb the processed network respose.

UPDATE AGAIN:

It won’t work, because the requestId is a userdata object, with two different pointer values!  That means that I cannot use them as keys to retrieve my target object from the global table!  I’m stumped.

-Ed

Hi Ed, I think you’re right, setup tables for easy network request operation then load the relevant table before each network request, assuming you are managing network requests individually. Agreed it could be with being added in future versions of the SDK. Cheers Chris.

Hi, thanks for the response.  I solved the issue by using an unique closure (one for each target object), as my networkListener function.  I still think it’s probably a good idea to add support for a delegate in network.request(), though.  It seems weird to use such an advanced concept as a closure to solve this problem.  Also, I think it probably wastes a little bit of memory, (because each target object has its own networkListener closure in memory).  That’s probably not a big deal, but it feels a little bit uglier than it needs to be.

-Ed

You need to use a closure because you are passing the address of the function to network.request().  Therefore you can’t pass parameters.  The system creates a table called event and passes it in.  The only way to do this is to use a closure, forwarding the event table along and then adding your parameters.  I know it feels hackish, but this is the way its designed.   The other thing you could do if you want your table method to work is to create a hash of the URL (so you will get a shorter unique string) to use as the key.  Then in your event handler, you can hash the event.url and use it as the lookup into your table.

Rob

Hi Rob,

Thanks for the quick reply.  The closure solution is working very well for me now.  Encoding the url would not work for my situation, because I’m using the same URL for different POST requests (with different headers/bodies).  It’s no matter, because the closures are working, and I’m starting to see the beauty in that solution to the problem :).

However, it might be worth mentioning this problem and the closure solution on the network.request() page in the docs?  People coming from the Obj-C iOS developing world (such as myself), are accustomed to using delegates for asynchronous communications.  When we submit the POST request, we pass an address of a delegate object to receive the data and download status messages.

Anyway, I’m a Corona (and Lua) newbie, but I’m already enjoying the Corona SDK so far.  Thank you guys.

-Ed

Actually, we just did a tutorial this week on Anonymous Functions/Closures.  I don’t know that I specifically mentioned network.request(), but…  You can read it here:

http://coronalabs.com/blog/2014/02/18/tutorial-anonymous-functions-and-closures/

UPDATE: I just thought of a workaround, but it’s a bit ugly.

I will use the return value of network.request() as the key in a global table that maps to the “target object” that will absorb the processed network respose.

UPDATE AGAIN:

It won’t work, because the requestId is a userdata object, with two different pointer values!  That means that I cannot use them as keys to retrieve my target object from the global table!  I’m stumped.

-Ed

Hi Ed, I think you’re right, setup tables for easy network request operation then load the relevant table before each network request, assuming you are managing network requests individually. Agreed it could be with being added in future versions of the SDK. Cheers Chris.

Hi, thanks for the response.  I solved the issue by using an unique closure (one for each target object), as my networkListener function.  I still think it’s probably a good idea to add support for a delegate in network.request(), though.  It seems weird to use such an advanced concept as a closure to solve this problem.  Also, I think it probably wastes a little bit of memory, (because each target object has its own networkListener closure in memory).  That’s probably not a big deal, but it feels a little bit uglier than it needs to be.

-Ed

You need to use a closure because you are passing the address of the function to network.request().  Therefore you can’t pass parameters.  The system creates a table called event and passes it in.  The only way to do this is to use a closure, forwarding the event table along and then adding your parameters.  I know it feels hackish, but this is the way its designed.   The other thing you could do if you want your table method to work is to create a hash of the URL (so you will get a shorter unique string) to use as the key.  Then in your event handler, you can hash the event.url and use it as the lookup into your table.

Rob

Hi Rob,

Thanks for the quick reply.  The closure solution is working very well for me now.  Encoding the url would not work for my situation, because I’m using the same URL for different POST requests (with different headers/bodies).  It’s no matter, because the closures are working, and I’m starting to see the beauty in that solution to the problem :).

However, it might be worth mentioning this problem and the closure solution on the network.request() page in the docs?  People coming from the Obj-C iOS developing world (such as myself), are accustomed to using delegates for asynchronous communications.  When we submit the POST request, we pass an address of a delegate object to receive the data and download status messages.

Anyway, I’m a Corona (and Lua) newbie, but I’m already enjoying the Corona SDK so far.  Thank you guys.

-Ed

Actually, we just did a tutorial this week on Anonymous Functions/Closures.  I don’t know that I specifically mentioned network.request(), but…  You can read it here:

http://coronalabs.com/blog/2014/02/18/tutorial-anonymous-functions-and-closures/