AngularJS同步$ http调用和数据库插入

时间:2015-12-10 07:15:50

标签: android angularjs sqlite cordova

我们正在使用离子的Android应用程序,我正在使用cordova s​​qlite db。 我正在进行多次$ http调用以从不同的api获取数据。我在数据库中插入每个api的数据。问题是,当我调用第一个api并在db中插入数据时,在完全插入第一个api数据之前调用下一个ajax。在ios平台上,它会启动内存问题并强行关闭应用程序。真的很期待这个问题可以在很多天内解决。

self.setApm_reference_t = function()
{
    $http.get(ApiEndpoint.url+'/references')
                  .success(function(response) {
                      console.log(JSON.stringify(response));
                      for(var i=0;i<response.length;i++){
                      $cordovaSQLite.execute(db, "INSERT INTO apm_reference_t (reference_id, apm_id, salutation, firstname_vc, middlename_vc, lastname_vc, designation_vc) VALUES(?, ?, ?, ?, ?, ?, ?)", [response[i].reference_id, response[i].apm_id, response[i].sal_vc, response[i].firstname_vc, response[i].middlename_vc, response[i].lastname_vc, response[i].designation_vc]).then(function(result){
        console.log("inserting in setApm_reference_t");
    },function(err){console.log(JSON.stringify(err));});
            if(i == response.length-1)
                {
                    self.setApm_contact_t();
                }
                      }
                      }).error(function(error){
        console.log(JSON.stringify(error));
        self.setApm_reference_t();
    });
},
self.setApm_contact_t = function()
{
    $http.get(ApiEndpoint.url+'/apmContacts')
          .success(function(response) {
              console.log(JSON.stringify(response));
              for(var i=0;i<response.length;i++){
                  $cordovaSQLite.execute(db, "INSERT INTO apm_contact_t (apm_contact_id, apm_id, tel_vc, reference_id) VALUES(?, ?, ?, ?)", [response[i].apm_contact_id, response[i].apm_id, response[i].tel_vc, response[i].reference_id]).then(function(result){
                        console.log("inserting in setApm_contact_t");
                    },function(err){console.log(JSON.stringify(err));});
                    if(i == response.length-1)
                    {
                        self.setDepartment_t();
                    }
                  }
              }).error(function(error){
        console.log(JSON.stringify(error));
        self.setApm_contact_t();
    });
},
self.setDepartment_t = function()
{
    $http.get(ApiEndpoint.url+'/departments')
          .success(function(response) {
              console.log(JSON.stringify(response));
              for(var i=0;i<response.length;i++)
              {
                  $cordovaSQLite.execute(db, "INSERT INTO department_t(department_id, departmentHeader_id, department_vc, departmentMr_vc, is_deleted) VALUES(?, ?, ?, ?, ?)", [response[i].id, response[i].departmentHeader_id, response[i].department, response[i].departmentMr, response[i].is_deleted]).then(function(result){
                        console.log("inserting in setDepartment_t");
                    },function(err){console.log(JSON.stringify(err));});
                    if(i == response.length-1)
                    {
                        self.setContact_t();
                    }
              }
              }).error(function(error){
        console.log(JSON.stringify(error));
        self.setDepartment_t();
    });
},
self.setContact_t = function()
{
    $http.get(ApiEndpoint.url+'/contacts')
          .success(function(response) {
              console.log(JSON.stringify(response));
              for(var i=0;i<response.length;i++){
                    if(response[i].insert_date == null)
                    {
                        response[i].insert_date = "0000-00-00 00:00:00";
                    }
                    $cordovaSQLite.execute(db, "INSERT INTO contact_t (contact_Id, name_VC,nameEn_VC, designation_short_VC, contact_IN, insert_date, update_date, delete_date, is_cmo) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)", [response[i].id, response[i].name_VC, response[i].nameEn_VC, response[i].designation_short_VC, response[i].contact_IN,  response[i].insert_date, response[i].update_date, response[i].delete_date, response[i].is_cmo]).then(function(result){
                        console.log("inserting in setContact_t");
                    },function(err){console.log(JSON.stringify(err));});
                    if(i == response.length-1)
                    {
                        self.setApm_location_t();
                    }
                  }
              }).error(function(error){
        console.log(JSON.stringify(error));
        self.setContact_t();
    });
},

1 个答案:

答案 0 :(得分:2)

您的问题的一般答案是返回承诺和

self.setContact_t = function() {
    //save httpPromise for chaining
    var p1 = $http.get(ApiEndpoint.url+'/contacts')
          .then(function(response) {
                console.log(JSON.stringify(response.data));
                return response.data;
        }).catch(function(error){
                console.log(JSON.stringify(error.data));
                //don't recursively call self on error
                //self.setContact_t();
                throw error;
        });
    //chain from httpPromise
    var p2 = p1.then ( function (dataList) {
        var p = $q.when();
        //iterate dataList items
        angular.forEach(dataList, function(data) {
            if (data.insert_date == null) {
                data.insert_date = "0000-00-00 00:00:00";
            };
            //chain items
            p = p.then( function () {
                        console.log("inserting in setContact_t");
                        return $cordovaSQLite.execute(db, insert(data));
            });
        };
        //return promise for chaining
        return p;
    });
    //chain again for setApmLocation
    var p3 = p2.then ( function () {
                         return self.setApm_location_t();
    });      
   //return promise for further chaining
   return p3;
};

请注意使用.then.catch方法,并且返回的数据与.success.error不同。