Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-24648] iOS: Network Activity Indicator does not work for Synchronous Requests

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionFixed
Resolution Date2017-05-22T12:50:49.000+0000
Affected Version/sn/a
Fix Version/sRelease 6.2.0
ComponentsiOS
Labelsnetwork
ReporterNicholas Thurston
AssigneeHans Knöchel
Created2017-05-01T20:36:25.000+0000
Updated2017-05-23T12:09:09.000+0000

Description

Network activity indicator only works for the first network call. Subsequent network calls do not show the network activity indicator. I traced this through xcode and it looks like synchronous requests are calling TiApp stopNetwork twice. In TiNetworkHTTPClientProxy: in send
[[TiApp app] startNetwork];
    if(async) {
        [httpRequest setTheQueue:operationQueue];
        [httpRequest send];
    } else {
        [httpRequest setSynchronous:YES];
        [httpRequest send];
        NSLog(@"Calling Stop Network from sync");
       [[TiApp app] stopNetwork];
        [self forgetSelf];
    }
In request onLoad..
-(void)request:(APSHTTPRequest *)request onLoad:(APSHTTPResponse *)response
{
    NSLog(@"Calling Stop Network from request onLoad");
    [[TiApp app] stopNetwork];
    if([request cancelled]) {
        [self forgetSelf];
        return;
    }
    NSInteger responseCode = [response status];
    /**
     *    Per customer request, successful communications that resulted in an
     *    4xx or 5xx response is treated as an error instead of an onload.
     *    For backwards compatibility, if no error handler is provided, even
     *    an 4xx or 5xx response will fall back onto an onload.
     */
    if (hasOnerror && (responseCode >= 400) && (responseCode <= 599)) {
        NSMutableDictionary * event = [TiUtils dictionaryWithCode:responseCode message:@"HTTP error"];
        [event setObject:@"error" forKey:@"type"];
        [self fireCallback:@"onerror" withArg:event withSource:self withHandler:^(id result){
            [self forgetSelf];
        }];
    } else if(hasOnload) {
        NSMutableDictionary * event = [TiUtils dictionaryWithCode:0 message:nil];
        [event setObject:@"load" forKey:@"type"];
        [self fireCallback:@"onload" withArg:event withSource:self withHandler:^(id result){
            [self forgetSelf];
        }];
    } else {
        [self forgetSelf];
    }
}
My Log from a one synchronous request
2017-05-01 16:19:16.658 My App[98152:872410] Start Network
2017-05-01 16:19:16.867 My App[98152:872651] Calling Stop Network from request onLoad
2017-05-01 16:19:16.868 My App[98152:872410] Stop Network
2017-05-01 16:19:16.868 My App[98152:872651] Calling Stop Network from sync
2017-05-01 16:19:16.868 My App[98152:872410] Stop Network

Comments

  1. Sharif AbuDarda 2017-05-02

    Hello, Can you share a reproducible code and steps to test the issue in our environment? Thanks.
  2. Hans Knöchel 2017-05-02

    PR: https://github.com/appcelerator/titanium_mobile/pull/9006 Test-Case (Sync-Request with 2sec delay to see the loader):
       var win = Ti.UI.createWindow({
           backgroundColor: '#fff'
       });
       
       var btn = Ti.UI.createButton({
           title: 'Trigger'
       });
       
       btn.addEventListener('click', function() {
           var url = "https://httpbin.org/delay/2";
           var client = Ti.Network.createHTTPClient({
               // function called when the response data is available
               onload : function(e) {
                   Ti.API.info("Received text: " + this.responseText);
                   alert('success');
               },
               // function called when an error occurs, including a timeout
               onerror : function(e) {
                   Ti.API.debug(e.error);
                   alert('error');
               },
               async: false,
               timeout : 15000  // in milliseconds
           });
           // Prepare the connection.
           client.open("GET", url);
           // Send the request.
           client.send();
       });
       
       win.add(btn);
       win.open();
       
  3. Harry Bryant 2017-05-23

JSON Source