make sure httpClient onload function finished
how can i assure that the onload function of a httpclient is executed completely before the app continues with other stuff?
i want to download ten xml-files but i am not interested in ten different httpclients, so i want to wait until the download finishs and then start the next one.
5 Answers
-
i found a great asynchronous solution http://developer.appcelerator.com/question/35991/multiple-httpclient-get-requests-asynchronous#72411
-
you can fire an event when the httpClient request has finished.
the receiver of the event can then initiate a new httpClient request.
I can provide additional details in the suggestion is not clear.
-
This is UNTESTED, and I am not sure I am folowing you, but would something like this work?
var count = 0; var receivedFiles = []; //fire off all requests request(1, callback); request(2, callback); ... request(10, callback); function callback() { //results come back in any order //only continue after last result receivedFiles.push('the file'); count++; if (count >= 10) {doRemainingStuff()} } function doRemainingStuff() { //the rest of the logic is inside this function }
-
Hi 'dev'
There is a 'synchronous' mode for HTTPClient. I believe it's enabled as follows…
xhr.open("GET","http://myservice/data.json",true);
(you can check that in / iphone / Classes / TiNetworkHTTPClientProxy.m under https://github.com/appcelerator/titanium_mobile)
I use asynchronous with a 'collector' function to establish when all are done, as Stan described above. But then I only have 3-6 requests and prefer to fire them asap to reduce wait time.
cheers,
Chris. -
nice idea stan, but no idea what "request" needs to look like.. the problem is to fire that callback event i think