Hey guys,
I am currently developing an android mobile application in Titanium Studio 3.2.1 and working with Titanium SDK 3.2.1 on Win 7 Professional x64 machine with JDK 1.7 installed. I am currently testing app with Android 4.2 emulator.
So for the app, I have created a small module which fetches data from a database via a webservice call and this data is used to create tableData which is then passed in app.js during function call, and that table data is used to create a table. Something along this line:
//mymodule1 var tableData = []; exports.refreshTableData = function() { var url = "http:example.com/webservice/rest/methodpath"; var json; var xhr = Titanium.Network.HTTPClient({ onload: function(e) { json = JSON.parse(this.textResponse); for (var i = 0; i < json.length; i++) { var row = Ti.UI.createTableViewRow({ //...some lines //... }); tableData.push(row); Ti.API.info("2"); } Ti.API.info("3") }, onerror: function(e) { Ti.API.info("ERROR: " + e.error); } }); Ti.API.info("4"); xhr.open("GET", url); xhr.send(); } exports.fetchTableData = function() { return tableData; }
This is what my app.js is like
//app.js var module1 = require('/lib/mymodule1'); var win1 = Ti.UI.createWindow({backgroundColor:'#fff'}); var table1 = Ti.UI.createTableView(); Ti.API.info("1"); module1.refreshTableData(); Ti.API.info("5"); var tableData = module1.fetchTableData(); Ti.API.info("6"); table1.setData(tableData); win1.add(table1); win1.open();So problem is that table data here is returned empty before httpclient gets chance to retrieve data from database and window stays empty. What I mean is that 1, 4 , 5 and 6 get executed before 2 and 3 does. The fetch function returns even before httpclient onload function has finished. I know that unlike iOS, on Android I can't set async property to false to make it work in proper order. So I want to know is there any workaround for this so I can execute in proper order?
Thanks