lundi 31 août 2015

How to make looping through SQLite result synchronous

I'm trying to insert the records from the JSON file to SQLite DB. After which I will redirect the user to required page.

Here is my code:

.factory('loadAutoComplete', function($q){
 var AutoComplete = {};

     AutoComplete.loadData = function(){

        try
        {
            var deferred = $q.defer();
            var response = {};

            db.transaction(function(tx) {

                    LocationList.forEach(function(loc){
                                         var query = "SELECT * FROM  tbl_Location WHERE ID = ? AND VALUE = ?";
                                         tx.executeSql(query,[loc.id, loc.value],function(tx, res){
                                                       if(res.rows.length == 0){
                                                          tx.executeSql("INSERT INTO tbl_Location (id, value) VALUES (?, ?)", [loc.id, loc.value],
                                                                        function(tx, res){
                                                                        },
                                                                        function(e){ console.log("Error: " + e.message); });
                                                             }

                                                       }, function(e){ console.log("Error: " + e.message); });
                                         });

                    tx.executeSql("SELECT COUNT(*) AS Count FROM tbl_Location", [], function(tx, res){
                                if(res.rows.length > 0)
                                    console.log("New rows added to tbl_Location:" + res.rows.item(0).Count);
                                });

                    response.success = true;

                    deferred.resolve(response);
                });
            return deferred.promise;
        }
        catch(ex)
        {
            console.log("Error:" + e.message);
            response.success = false;
            deferred.resolve(response);
        }
     }

     return AutoComplete;
     })

In Controller:

var promise = loadAutoComplete.loadData();
promise.then(function(responseData){
      $ionicLoading.hide();
      flag = response.result.toString();

      if(flag==true || flag=="true")
      {

                                                LogInService.setUserName(user.username);
                                                LogInService.fromLogin = true;
                                                if(UserInfoService.getInfo().userinfopagevisited=="yes")
                                                {
                                                    var view = $ionicViewService.getCurrentView();
                                                    $scope.$viewHistory.forcedNav = {
                                                        viewId: view.viewId,
                                                        navAction: 'moveForward',
                                                        navDirection: 'forward'
                                                    };
                                                    $state.go("incidents-list");
                                                }
                                                else
                                                {
                                                    var view = $ionicViewService.getCurrentView();
                                                    $scope.$viewHistory.forcedNav = {
                                                        viewId: view.viewId,
                                                        navAction: 'moveForward',
                                                        navDirection: 'forward'
                                                    };
                                                    $state.go("user-info");
                                                }
                                              }
                                              else
                                              {
                                                navigator.notification.alert(
                                                        $filter('translate')('login_Invalid_User_or_Password_errormsg'),
                                                        alertDismissed,
                                                        alertBox_Heading_text,
                                                        alertBox_OKButton_text
                                                );

                                              }
                                    });

Problem:

User is redirected prior to the completion of data insertion.

Required Behavior:

User should be redirected after the logic of data insertion.

Any idea where it is going wrong.

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire