makewebrequest timeout handling

anybody figured out a way to still get data back from datafield background processing even in case of a timeout?

i've been doing both a webrequest and getting the device temperature in the same background call.  but if the webrequest times out, then i don't have a way to get the device temp either.

here's the common (and obviously not working just for show) code.  problem is the receiveResponse method won't ever get called in a timeout.  and you can't set a property in background, so that won't work either.

.

function onTemporalEvent() {  
    	
    Comm.makeWebRequest(
        url,
        null,
        {
            :method=>Comm.HTTP_REQUEST_METHOD_GET
        },
        method(:receiveResponse)
    );
}

    
function receiveResponse(responseCode, data)
{ 
	if(data != null)
	{
    	data["gt"] = getGarminTemp();
	
		Background.exit(data);
	}
}

  • check the response code.  If 200 return an array with the two values ([data,temp]), if not just return the one value ([null,temp])  Easy to check in onBackgroundData for the null.  Another option is instead of null, return the responseCode and that way you can see the error in the main app.

  • Ah!  I didn’t realize that the method would still get called even with a timeout. That makes it easy enough from there. Thanks Jim!

  • The basic thing I do in all my apps that do comm in the background is

    funcrion onReceive(responseCode,data) {
      if(responseCode==200) {Background.exit(data);}
      else {Background.exit(responseCode);}
    }

    then in onBackgroundData, this:

    function onBackgroundData(data) {
      if(data instanceof Integer) {
        //handle an error, maybe display it, etc
      } else {
        //got real data to handle
      }
    }

    as the onRecieve should always be called, error on the makeWebRequest or not (timeout, no connection, etc).  By passing the error back in the event of an error, it makes it easier to help a user having an issue, as you can ask them what the error is.