Skip to content

Instantly share code, notes, and snippets.

@kueda
Created February 1, 2011 20:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kueda/806630 to your computer and use it in GitHub Desktop.
Save kueda/806630 to your computer and use it in GitHub Desktop.
This is the method I'm using to send photos to iNat. It seems to die with a memory allocation error or a timeout error intermittently, especially over 3G.
function syncPhoto(photo, options) {
var options = options || {};
var credentials = Ti.App.Properties.getString('currentUserCredentials');
var observation = photo.getObservation();
var f = photo.getFile();
var xhr = Titanium.Network.createHTTPClient({
onerror: function(e) {
Ti.API.info("ERROR " + e.error);
if (e.error.match(/timed out/)) {
alert('Request timed out. You might want to try it again on a WiFi network. (' + e.error + ')');
} else {
alert("Sorry, something went wrong syncing your photo (" + e.error + ")");
},
onload: function() {
switch (this.status) {
case 200:
var now = new Date();
photo.updateAttributes({'synced_at': now});
break;
default:
alert('iNaturalist.org was unable to save that photo: ' + this.responseText);
showSync();
}
}
});
xhr.open('POST', iNat.HOST + '/observation_photos.json');
xhr.setRequestHeader('Authorization','Basic '+credentials);
xhr.setTimeout(1000*60*5); // photos can take a long time
xhr.send({
'file': f.read(),
'observation_photo[observation_id]': observation_id
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment