[TIMOB-17403] iOS: final ondatastream event should be fired before final onreadystatechange event
| GitHub Issue | n/a |
|---|---|
| Type | Improvement |
| Priority | Low |
| Status | Open |
| Resolution | Unresolved |
| Affected Version/s | Release 3.3.0 |
| Fix Version/s | n/a |
| Components | iOS |
| Labels | n/a |
| Reporter | Chris Barber |
| Assignee | Unknown |
| Created | 2014-07-26T00:14:23.000+0000 |
| Updated | 2018-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.
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.