[TIMOB-24648] iOS: Network Activity Indicator does not work for Synchronous Requests
| GitHub Issue | n/a |
|---|---|
| Type | Bug |
| Priority | Critical |
| Status | Closed |
| Resolution | Fixed |
| Resolution Date | 2017-05-22T12:50:49.000+0000 |
| Affected Version/s | n/a |
| Fix Version/s | Release 6.2.0 |
| Components | iOS |
| Labels | network |
| Reporter | Nicholas Thurston |
| Assignee | Hans Knöchel |
| Created | 2017-05-01T20:36:25.000+0000 |
| Updated | 2017-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
Hello, Can you share a reproducible code and steps to test the issue in our environment? Thanks.
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();