Success?! I also need several Request and always end up with errors...
thanks for your answer
you have to be aware that the callbacks are asynchronously and thus are not in guaranteed order (some may actually never end up in the callback), so before you force an exit from the background you either have to count the amount of callbacks received (and force exit when the amount matches the number of requests) or alternatively you can also chain the webrequests (call the 2nd webrequest from your 1st callback).
Personally I prefer method 1 (counting), but you can try both approaches and see what suits you best.
The best way to do this is serially. Make request 1, and in the callback for that, make request 2 and so on. Doing them all at once can cause issues. There's a max of 3 outstanding requests, but android doesn't handle that well.
I got problems with the order of all the steps. At the end theres one data package i will send to the main function(onbackgrounddata). Thats a bit too complicated for me right now...
Maybe one of you pros can text me an example of the basic code construction!? I‘ve spent a lot of time till today with that problem.
thanks so far
class MyServiceDelegate extends Sys.ServiceDelegate { var res1=null,res2=null; function onTemporalEvent() { ... Comm.makeWebRequest(url1,param1,options1,method(:onReceive1)); ... } function onReceive1(responseCode, data) { res1=data; ... Comm.makeWebRequest(url2,param2,options2,method(:onReceive2)); ... } function onReceive2(responseCode, data) { res2=data; ... Background.exit([res1,res2]); }
lots missing here like what you do with errors, but the basics.
I will test it as soon as possible. Thank you very much!
Check! Thanks for your help!
This seems to work fine @jim_m_58 - however, I return in res1 and res2 HTTP_RESPONSE_CONTENT_TYPE_JSON, so it will contain one object res1 in JSON and the second one res2 in JSON. How can I concatenate these objects correctly (like res1+res2)?
I would like to store the values in onBackgroundData(data)
function onBackgroundData(data) { var pendingWebRequests = getPendingWebRequest(); var type = data.keys()[0]; // Type of received data. var storedData = getProperty(type); var receivedData = data[type]; // The actual data received: strip away type key. // New data received: clear pendingWebRequests flag and overwrite stored data. storedData = receivedData; pendingWebRequests.remove(type); setProperty("PendingWebRequests", pendingWebRequests); setProperty(type, storedData); Ui.requestUpdate(); }
I call in OnReceive2
Bg.exit({"OpenWeatherMapCurrent" => responseCode != 200 ? { "httpError" => responseCode } : [res1, res2]});
Any hints or clues?