storesafe / cordova-sqlite-storage

A Cordova/PhoneGap plugin to open and use sqlite databases on Android, iOS and Windows with HTML5/Web SQL API
Other
2.15k stars 715 forks source link

When used with a promise in angularJS ui-router 'resolve', causes an infinite digest loop #179

Open ghost opened 9 years ago

ghost commented 9 years ago

I am trying to work out if I am approaching this the wrong way or it is a bug.

Using angularjs with ui-router, I am making an SQLlite call within the resolve part of the ui-router state declaration so the data needed from the DB is available to my view before it loads.

I have made three tests to probe the issue. The first returns simple synchronous data. The second returns asynchronous data using angular's built in $http method. The last does the same using cordova SQLlite but fails and causes an infinite digest loop.

Can you shed any light on what the issue might be as I have tried all the sensible approaches I can think of the address this?

02-04 13:00:34.310: D/CordovaLog(11304): file:///android_asset/www/lib/ionic/js/ionic.bundle.js: Line 19387 : Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! 02-04 13:00:34.280: D/CordovaLog(11304): Watchers fired in the last 5 iterations: [] 02-04 13:00:34.280: D/CordovaLog(11304): http://errors.angularjs.org/1.3.6/$rootScope/infdig?p0=10&p1=%5B%5D

The code I am using is as follows:

state:

.state("menu", {
    templateUrl: "templates/menu.html",
    controller: "MenuCtrl",
    //  ":id"  is a path parameter.
    url: "/menu",

      resolve:{
         simpleObj:  function(){
            return {value: 'simple!'};
         },
         httpTest:  function($http){
            return $http({method: 'GET', url: 'http://google.com'})
               .then (function (data) {
                   return data;
               });
         },
         sqliteTest:  function(){

            var deferred = $q.defer();
            query = "SELECT * FROM reports";
            $cordovaSQLite.execute(db, query, []).then(function(res) {
                  deferred.resolve(res); 
            }, function (err) {
                  console.log('error');
                  deferred.reject(err);
            });
            return deferred.promise;

         }
      }
})

controller:

angular.module('starter.controllers').controller('MenuCtrl', function($scope, simpleObj, promiseObj, sqliteTest) {

  $scope.simpleObj = simpleObj;
  console.log(JSON.stringify($scope.simpleObj));

  $scope.promiseObj = promiseObj;
  console.log(JSON.stringify($scope.promiseObj));

  $scope.sqliteTest = sqliteTest;
  $scope.sqliteTest.then(function(res) {
  console.log(1);

      for (var i = 0; i < res.rows.length; i++) {
         console.log(res.rows.item(i).id);
      }

  }, function() {
      console.log(2);
  });

});

mladenp commented 9 years ago

Did you fix this? I just moved db initialization to ui routers resolve and i got the same loop error.

brodycj commented 9 years ago

As described in the readme, the app must wait for the deviceready event before attempting to open the db.

mladenp commented 9 years ago

I was just reading about this infinite digest loop, and it seems that there is a bug when using $stateProvider and $urlRouterProvider.otherwise. But you are right i totally forgot about this plugin depending on deviceready.