clearCache() possible bug?

@seanhill @cquartier I’m also seeing this issue on beta 14 and beta.14-nightly-1060.

My app is based off ionic-starter-tabs. I have 4 tab states and a separate state for login. I’m calling $ionicHistory.clearCache() and $ionicHistory.clearHistory() from a controller within one of the tabs. For me, it removes all the cached tabs from the DOM except for the tab which logout is called from.

So when logging back in, that tab remains cached and displays old user data.

@mhartington

When you have cached views enabled, the controller does not run again. even if you clear cache with $ionicHistory.clearCache();

I think it is clearly a bug.
I hope it helps :wink:

I was able to work around my problem case by calling $ionicHistory.clearCache() and $ionicHistory.clearHistory() on logout AND login.

When the user logs out from the settings page and gets taken to the login page, the settings page is still cached. When another user logs in I clear cache again which successfully removes the cached settings view.

1 Like

@bostondv thanks for suggestion

Hi everyone. Is there any solution for this problem? I also cant clear cache by $ionicHistory.clearCache();. I tried do this on user logout, then on logout AND login. Nothing changes, the page on which the user appears after login still show cached data. Any suggestions?

Hi! I don’t think it will help you, but I deactivated completely the cache. As long as there are so many people that don’t understand how it’s said to work, and it’s not doing things logically, as far as I’m concerned it’s not production ready.

Good luck!

It seems $ionicHistory.clearCache() does not work alone. I have to combine $ionicHistory.clearCache() and $ionicHistory.clearHistory() to make it work.

Have this issue been fixed? Because i’m using ionic 1.0.0 and when i use clearCache() i think it clears other views except the current view. Could you suggest me any trick to deal with it?

Check this issue.

We can try this:

$scope.logout = function() {
     $state.go('...');
    // Clear all cache and history
    $timeout(function () {
        $ionicHistory.clearCache();
        $ionicHistory.clearHistory();
    }, 1500)
  }

Note: Remember include $timeout and $state in controller options

3 Likes

Hi Friends, I hope this will works for after user logout the back button will be disabled.

$ionicHistory.nextViewOptions({
disableAnimate: true,
disableBack: true
});

1 Like

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();
  });
1 Like

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…

1 Like

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??

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:

2 Likes

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

3 Likes

This worked for me! Thanks for the tip!

Thanks a lot

this code save me

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.

1 Like

This is a life saver!

1 Like