clearCache() possible bug?


#21

Hi all,

I found out that (for me) the only solution that did work is by using the ionicView.enter in the controller.

Considering your logout will of course redirect you to some “home page” or “login page”, then you can put the following code in its controller:

$scope.$on("$ionicView.enter", function(scopes, states){
		$ionicHistory.clearHistory();
		$ionicHistory.clearCache();
  });

#22

Hey guys,

I don’t meet any problems now with clearcache. The simple trick is to go to the next state only after clearcache’s promise resolved. Like so :

$ionicHistory.clearCache().then(function () {
    // Do... Whatever it is you do (if needed)
    $state.go("mystate");
});

Display a loader or something when waiting for it to complete if needed. Like that it works perfectly…


#23

Did you find any solution?

I tried clearing $ionicHistory.clearCache();$ionicHistory.clearHistory() and localstorage . Afte this when I redirect using $state.go(‘app.home’) it takes an eternity to change states which gives an impression that the app hangs.

Any help??


#24

Wow, This is the right answer. That issue was happening because when we logout from some state, and call $ionicHistory.clearCache(), we are still on that state, and by moving to login screen, this settings screen is stored in the history… Thanks for the help… :slightly_smiling:


#25

YEAP! the $timeout function is the right answer!! Thank You Phan! :+1:


#26

This worked for me! Thanks for the tip!


#27

Thanks a lot

this code save me


#28

Well this is an old issue, but for anyone that’s coming 2017 or later I will explain what really happens and how to solve it:

The code of $ionicHistory.clearCache():
clearCache: function(stateIds) { return $timeout(function() { $ionicNavViewDelegate._instances.forEach(function(instance) { instance.clearCache(stateIds); }); }); },
So, as you can see, it takes 1 parameter cllaed stateIds which is an array of stateId. Indeed i struggled to find out that stateId is nothing more than stateName.

So, let’s go deeper. The code of $ionicNavView.clearCache which is used in the line above “instance.clearCache(stateIds)” is:

self.clearCache = function(stateIds) { var viewElements = $element.children(); var viewElement, viewScope, x, l, y, eleIdentifier; for (x = 0, l = viewElements.length; x < l; x++) { viewElement = viewElements.eq(x);

  if (stateIds) {
    eleIdentifier = viewElement.data(DATA_ELE_IDENTIFIER);

    for (y = 0; y < stateIds.length; y++) {
      if (eleIdentifier === stateIds[y]) {
        $ionicViewSwitcher.destroyViewEle(viewElement);
      }
    }
    continue;
  }

  if (navViewAttr(viewElement) == VIEW_STATUS_CACHED) {
    $ionicViewSwitcher.destroyViewEle(viewElement);

  } else if (navViewAttr(viewElement) == VIEW_STATUS_ACTIVE) {
    viewScope = viewElement.scope();
    viewScope && viewScope.$broadcast('$ionicView.clearCache');
  }

}

};
`

And as you can see in the code, this clearCache DOES NOT CLEAR ALL CACHES, instead, it destroy all cached views that matches a value in the stateIds array. If there’s no parameter IT JUST DESTROY THE ACTUAL VIEW.

So the solution for this, using just the Ionic way is to call $ionicHistory.clearCache() with all your state names in an array as parameter.

E.g:

$ionicHistory.clearCache(['login', 'map', 'home']);
I cannot belive any Ionic developer didnt dug into the code before, or missed this simple datail.
I Hope someone takes advantage of this, even being so late.


#29

This is a life saver!