Handling multiple http request

You must Login before you can answer or comment on any questions.

I am a beginner to the appcelerator and facing a problem.

I have multiple http requests similar to below which fetches more than hundred documents from the server. The responseText from the server comes within a few seconds but than the "for" loop in my code takes quite a lot of time to insert values and until than i have to wait. Any solution would really be appreciated

App.window_table.inspectionQuestionsData=function(){
 
    var xhr=Ti.Network.createHTTPClient({
        timeout:1000000
 
 
    var db = Ti.Database.open("inspectionQuestionsDb");
 
        db.execute('CREATE TABLE IF NOT EXISTS inspectionQuestionsTable (uniqueKey VARCHAR(40), Category TEXT, Question TEXT, Weight TEXT, Achieved TEXT,Score TEXT,Comments TEXT,RWFlag TEXT)');
        db.close();
 
    xhr.onload=function(){
 
            try {
 
                var data = this.responseText;
                //alert(data);
                data = data.split("\n");
 
                if(!data) {
                    Ti.API.log("getdata: cannot split JSON data: " + data.substr(0, 100));
                    return;
                }
 
 
                var db = Ti.Database.open("inspectionQuestionsDb");
                for(var i = 0; i < data.length; i++) {
                    if(data[i]) {
 
                        var item = JSON.parse(data[i]);
                        var uniqueKey = item.unid;//uniqueID
                        var values = new Array();
 
                        values.push(uniqueKey);
                        values.push(item.values[0]);
                        values.push(item.values[1]);
                        values.push(item.values[2]);
                        values.push(item.values[3]);
                        values.push(item.values[4]);
                        values.push(item.values[5]);
                        values.push('0');
 
                        sql = "INSERT INTO QuestionsTable (uniqueKey, Category,Question,Weight,Achieved,Score,Comments,RWFlag) VALUES (?,?,?,?,?,?,?,?)";
 
                            try {
 
                                db.execute(sql,values);
                            } catch (e) {
                                Ti.API.log("getData SQL UPDATE error: ", e);
                            }
 
                        }
 
                }
 
                db.close();
 
 
            } catch (e) {
                YN.log("xhr error: " + e);
            }
 
    }
 
    xhr.onerror=function(e)
    {Ti.API.log("Cannot read data 1 (httpClient error: " + e.error + ")");}
 
    xhr.open("GET",url);
 
    xhr.send();
    }
}

2 Answers

If you want to make several network calls at once, I suggest writing a module to handle your requests. This way, you can have multiple requests being handled independently. This would allow you to have more than one request processing at a time, as apposed to sequentially. For example:

NetworkRequest.js

var NetworkRequest = function(params){
     var callback = (params.callback) ? params.callback : null;
     var errorFunction = (params.errorFunction) ? params.errorFunction : null;
     var xhr = Ti.Network.createHTTPClient();
     xhr.open(params.method, params.url);
 
     xhr.onload = function(){
        if(callback){
            callback(JSON.parse(this.responseText));
        }
     };
 
     xhr.onerror = function(e){
         if(errorFunction){
            errorFunction();
        }
     };
 
     xhr.send();
};
module.exports = NetworkRequest;
app.js
var request = require('NetworkRequest');
 
var request_1 = new request({
     url:'http://mydomain/network/request_1',
     method:'GET',
     callback:function(returnedObject){
        // do something
     },
     errorFunction:function(returnedObject){
        Ti.API.error('Problem with request_1');
     }
});
 
var request_2 = new request({
     url:'http://mydomain/network/request_2',
     method:'GET',
     callback:function(returnedObject){
        // do something
     },
     errorFunction:function(returnedObject){
        Ti.API.error('Problem with request_2');
     }
});
Note that having too many processes happening at once could crash your app.

Blessings!

Eric

I am not completely sure on what you are aiming for, i can just guess a few solutions.

First things first, the httpClient is ASYNC, meaning you can set up many http request at the same time. So you can just create a few functions ( one httprequest functions and multiple callback functions is best ) call them at the same time and you have your awsner.

But, i see you are using a database and i am not sure if every httprequest is going to need the same database, if this is the case, then you should fire all functions, wait for each one to return data, merge data and then push the data into the database. OR you could fire first httprequest, then the second, third etc... keep filling up one data object and when the last one is done, you will fill up your database.

Hope this helps you and if this still does'nt awnser your question, please provide with more/better details so i can come up with a more specific awnse

Your Answer

Think you can help? Login to answer this question!