Programmatically clear cache for specific view

Is there a way to programmatically clear the cache for a view?

For example I have a view that upon loading fetches the person from a server:

person/:personId

On another view I perform updates to that person, persist them to server (and perform some server processing on updates) and navigate back to ‘person/:personId’. Since the view ‘person/:personId’ is cached the person is not refetched. Ideally I could programmatically do something similar to:

persistPersonToServer();
clearViewCacheFor(‘person/:personId’);
navigateTo(‘person/:personId’);

Thanks!

@KyleT You could listen on $ionicView.beforeEnter event in your view controller and fetch the data from server, before view becomes active. Something like this -

    .controller('ViewCtrl', function($scope) {
        $scope.$on('$ionicView.beforeEnter', function(){
            if ($scope.personUpdated) {
                $scope.person = fetchPerson(personId);
                $scope.personUpdated = false;
            }
        });
    });

I’ve been trying to solve the exact problem, let me know if you find a way to do so.

Thanks

Thanks @sreekanth!

@GaryTyre I used $scope.$on(’$ionicView.beforeEnter’ in combination with sessionStorage to solve my issue.

Similar to (pseudo-code):

.controller('PersonDetailCtrl', function($scope) {
    $scope.person = {};
    $scope.$on('$ionicView.beforeEnter', function(){
        if (window.sessionStorage.getItem('refresh-personId') && $scope.loaded) {
            $scope.person = $scope.fetchPerson(personId);
            window.sessionStorage.removeItem('refresh-personId') ;
        }
    });
    $scope.fetchPerson(personId);
});

Sorry, I think this is topic I need. My problem is I am getting a scrambled view when switch between running an App and the Intro instructions. I think it’s a caching problem. Here is a picture of what I am getting.

This is just a simple password generator and the problem happens when I switch from App to Intro. Does this sound like a cache problem? I notice when switching back to Intro the background is blue, which is background color of App. The Intro background should be white so somehow I am confusing the operation between App and Intro. If I need to post code I have no problem because all I am trying to do is release this as a free App. One more thing I notice is that the problem does not happen consistently but it is an issue. The ionic version I am using is 1.3.0 — thank you

I would also like to see the ability to clear cache for a certain view added in to Ionic core. I just had this need pop up last night. For now, I’m just using clearCache() and clearing the full cache. It seems a bit less clunky than having to rely on beforeEnter. Though I am also using beforeEnter in some places to make sure some of the data in my view is fresh.

can clear a specific view by using

$ionicHistory.clearCache([stateId]);

note: you can’t clear a page’s cache that you are currently on.

thx! your solution worked for me!

@soocheng $ionicHistory.clearCache([stateId]); does not work for me. Any reason for this? I am trying to clean the cache of another view I am not in.

1 Like

You can also disable cache for a specific state in the ui-router by setting the cache property to false:

            .state("profileEditAddress", {
                url : "/profileEditAddress/:id",
                controller : "ProfileEditAddressCtrl",
                cache : false,
                templateUrl : "components/profileEditAddress/profileEditAddress.html"
            })
1 Like

you can try to log the stateId of your another view,
the stateId of your view might be appended with its state params
eg:
your state name is ‘article’ with state params ‘id’,
your stateId might be article_id_blabla