clearCache() possible bug?


#1

Hello guys,

So I tried to use clearCache() in my app to clear all cached views whenever reaching a specific state. I tried with a function when the button leading to that state is clicked :

$scope.switchEvent = function() {
    $ionicHistory.clearCache();

    $state.go("welcome");
}

That call was from a nested state (called “app.main”). Didn’t work. When clicking on the link that gets me back to “app.main” with a different parameter for the page (should be different data), the cached view shows up, with the old data. Note that if using the inline

cache-view="false"

Directly on the view, it works.

So I tried something else. On the “.run” function, I added :

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
    if(toState.name == "welcome")
        $ionicHistory.clearCache();
});

Using this didn’t work either. The clearCache() was triggered (I checked it with a console.log) but didn’t clear any cached view. But if I used no condition and launch clearCache() on every view change, it worked. I ended up with this

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
    if(fromState.name.substr(0, 4) != "app." && (toState.name == "app.main" || toState.name == "app.mainSecondary"))
        $ionicHistory.clearCache();
});

So this works, but I’m not satisfied as it’s not very robust… Apparently only clearCache() when changing to the wanted view works. Not sure if it’s linked to the nested states or what, but there you go…

@adam I wanted to reference this issue in Please help test: Angular 1.3, improved transitions, cached views, etc but it’s closed :sunny:


Ionic - cache
#2

Hey @JerryBels I’m having a very similar issue. I have it in my logout function.

$scope.logout = function() {

	$ionicHistory.clearCache();
	$ionicHistory.clearHistory();
	$state.go('home');

};

However, after inspecting the DOM after my logout, there is still plenty of cached views, and I can still navigate backwards through my history, aka, it looks like those two functions did nothing at all.

@mhartington any help here?


#3

Hmm it does seem to work, but its a bit tricky.


#4

@mhartington my situation is a little different. Where the logout function is on my most parent ctrl, for instance a ctrl connected to the body tag. I want to be able to call logout anywhere in the app and be able to clear my history and cache, and just drop them back on my home screen. In your example you use the afterLeave event on a child controller to remove the history/cache. However, for me, maybe I don’t want to clear out the history/cache everytime a user navigates away from that view, but only when they use the logout functionality.

It seems like I’d have to duplicate a lot of code to be able to logout in different views if the clear history / cache isn’t in the most parent logout function.


#5

That’s pretty much what I do / want to do too.


#6

I will try it tomorrow and report how it went for me… But anyway it should work right away as in my example, no ?

Something else : the caching doesn’t take into account the $stateParams, which means the same page is displayed for user/24 and user/77 and that is bad. IMHO when navigating to a view that has stateParams, it should check them and if they’re not the same as in the cache, clear the cache for that view and display the new one (to avoid having a lot of the same view cached, only the last one should be kept).


#7

Okay, after testing… @seanhill +1


Upgrade from Beta 13 (Angular 1.2) to Beta 14 (Angular 1.3) is painfull
#8

@seanhill I have the same problem. For now I’m disabling the cache throughout my whole app because if I can’t reliably clear the cache on logout then I have data security concerns. I think the problem might be how the $ionNavViewDelegate instances are tracked in the framework. The $ionicHistory.clearCache function is looping through those instances and calling the clearCache function on each of them (which presumably should destroy the DOM elements and scopes for every view in your app). It seems like they might expect an instance per cached view but I’m only ever seeing 2 instances being cleared, the one where my logout function exists, and its parent.

It would be nice if we could get an Ionic/Driftyco developer to weigh in on this, if nothing else than to acknowledge that the cache-clearing mechanism doesn’t appear to be working the way most of us expect.


#9

For those interested I tried to help the devs here : https://github.com/driftyco/ionic/issues/2939#issuecomment-74427724

Since I don’t consider myself already really good with Angular, everyone is welcome to improve the demo :smile:


#10

@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.


#11

@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:


#12

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.


#13

@bostondv thanks for suggestion


#14

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?


#15

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!


#16

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


#17

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?


#18

Check this issue.


#19

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


#20

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

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