Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-17403] iOS: final ondatastream event should be fired before final onreadystatechange event

GitHub Issuen/a
TypeImprovement
PriorityLow
StatusOpen
ResolutionUnresolved
Affected Version/sRelease 3.3.0
Fix Version/sn/a
ComponentsiOS
Labelsn/a
ReporterChris Barber
AssigneeUnknown
Created2014-07-26T00:14:23.000+0000
Updated2018-02-28T20:04:15.000+0000

Description

The final ondatastream event containing progress=1 is fired after the DONE onreadystatechange event. This means you fall into a wonky state where the request is finished but you haven't been notified that all of the data has been received.
var xhr = Ti.Network.createHTTPClient();
xhr.setTimeout(6e4);
var dataStreamFinished = false;
xhr.onreadystatechange = function(e) {
	Ti.API.info('state = ' + this.readyState + ' (' + dataStreamFinished + ')');
	if (this.readyState == this.DONE && dataStreamFinished) finish();
};
xhr.ondatastream = function(e) {
	if (!e.progress) callback_error("Errors in ondatastream");
	Ti.API.info(e.progress);
	if (e.progress >= .9) dataStreamFinished = true;
};
xhr.onerror = function(e) {
	callback_error(e);
};
xhr.open("GET", "http://www.appcelerator.com/assets/The_iPad_App_Wave.pdf");
xhr.send();
Apparently, this is how the events are dispatched in native land. To resolve this issue, when the DONE onreadystatechange event is fired, disconnect the ondatastream event handler, manually fire the ondatastream event with progress=1, then continue to fire the DONE onreadystatechange event.

Comments

  1. Hans Knöchel 2016-07-02

    Correct me if I am wrong, but I guess we could just swap the lines in [here](https://github.com/appcelerator/APSHTTPClient/blob/master/APSHTTPClient/APSHTTPRequest.m#L537) and it should fit your expectations - which I share.

JSON Source