Long Running Network Tasks

kony.net.HttpRequest now supports long running network tasks which run to completion even when the app is pushed to Background or Suspended state, these are also known as background transfers and are available from iOS7.

Background transfers can be scheduled while the app is in foreground or while the app is in background (with the help of background fetch jobs in background state).

Background transfers can be enabled by setting the property ‘backgroundTransfer’ to ‘true’ on the HTTP request object returned by kony.net.HttpRequest.

‘‘<HttpRequestObject>.backgroundTransfer’ has to be set to ‘true’ before open() API is invoked on the HTTP request after its creation.

It is always advisable to set “<HttpRequestObject>.backgroundTransfer” to ‘true’ immediately after creation of HttpRequest object.

Syntax

var httpclient = new kony.net.HttpRequest(
   {
       "timeoutIntervalForRequest": 60,
       "timeoutIntervalForResource": 600
   });
httpclient.backgroundTransfer = true;

Input Parameters

Http request object returned by kony.net.HttpRequest supports the following additional properties to enable running network tasks to run even when the App is in background state.

  • backgroundTransfer [Boolean]: Lets the HTTP request to know that Background transfers need to be supported. The default value of this property is false.

    NOTE: This property is only available for the iOS platform.

  • timeoutIntervalForRequest [Number]: Define a time-interval for request how long a task (upload or download) should wait for new data to arrive. If the new data arrives within the specified interval, the timer associated with this property resets. If the new data does not arrive within the specified interval, it triggers timeout.

    NOTE: This parameter is available for the iOS and Android platforms.

    The default value of this property is 60 (in seconds).

  • NOTE: Any upload or download tasks created by a background session are automatically retried if the original request fails due to a timeout. To configure how long an upload or download task should be allowed to be retried or transferred, use the timeoutIntervalForResource property.

  • timeoutIntervalForResource [Number]: Define a time-interval for resource how long to wait for entire resource to arrive. The resource timer starts when the request is initiated and counts until either the request completes or this timeout interval is reached, whichever comes first. The default value of this property is 1 week (7 days).

    NOTE: This parameter is available for the iOS and Android platforms.

Example


var logPrefix = "bg_transfer_test_case_1 :- ";
    send_request();
    
    function send_request() {
        var request = new kony.net.HttpRequest();
        request.onReadyStateChange = callbackHandler;

        //BGtransfer
        request.backgroundTransfer = true;

        //session
        var req_session = request.getSession();
        kony.print(logPrefix + " request.getSession() : " + req_session);
        //url
        var url = "https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/iphoneappprogrammingguide.pdf";
        kony.print(logPrefix + "URL : " + url);
        request.open(constants.HTTP_METHOD_GET, url);
        request.send();
    };
    
    function callbackHandler(request) {
        kony.print(logPrefix + " : ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        kony.print(logPrefix + " : Scope :- request.status : " + request.status);
        kony.print(logPrefix + " : Scope :- request.statusText : " + request.statusText);
        kony.print(logPrefix + " : Scope :- request.responseType : " + request.responseType);
        kony.print(logPrefix + " : Scope :- request.response : " + request.response);
        kony.print(logPrefix + " : Scope :- request.getAllResponseHeaders() : ");
        kony.print(request.getAllResponseHeaders());
        kony.print(logPrefix + " : ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    };

Platform Availability

Available for the iOS and Android platforms.