Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to clear the view cache in Ionic?

I'm currently working on a Angular/Ionic/Cordova project and we've recently upgraded to the latest Ionic beta. From the version the project was using before, this introduced the view cache. It has also however introduced an issue in doing so.

The application is customer-facing and is very data-centric. A user must authenticate to view data associated with their account, currently however; when a user logs out, and logs into another account, because the view(s) are still cached they are presented with the views of the last account.

The app should still cache the views when the user is logged in, as it helps make the app feel much quicker, but the cache should be purged when a user logs out.

Setting cache-view="false" is not an option, as it would completely disable the cache.

I've also tried setting $ionicConfig.views.maxCache(0); and then back to the default of 10 in the hopes that it would purge the cache in doing so, but it had no effect.

The last thing I can think of to do is fire an event when a user logs in that refreshes all data that's currently loaded into the views - however, this would take a bit more effort than I feel it should.

Is there a way to simply clear the view cache?

like image 334
Seer Avatar asked Feb 23 '15 14:02

Seer


People also ask

How do I clear cache in ionic?

logout = function() { $ionicHistory. clearCache(); $ionicHistory. clearHistory(); $state.go('home'); };

What is the advantage of caching the views in ionic apps?

What is the advantage of caching the views in Ionic apps? Provide examples. Ionic by default caches up to ten views, which improves performance and also maintains different states in the views at the same time. For example, the cache can maintain scroll position in the views or active state of buttons.

What is clear caching?

What Does it Mean to Clear Cache? Clearing your cache means deleting the information automatically stored to your device when visiting a new site or opening an app. You might do this if you are strapped for space on your device or if you've noticed it's performing slower than usual.

What is angular cache?

A cache object used to store and retrieve data, primarily used by $templateRequest and the script directive to cache templates and other data. angular.


1 Answers

In the state definition in app.js you can add cache:false to disable caching (See Disable cache within state provider in the Ionic docs. Or, you can keep caching except when you know data has changed.

  • If you're already on the page, you can do, $state.go($state.currentState, {}, {reload:true})
  • If you're on another state and want to go back to the state that is normally cached but you want it to be refreshed, you can do, $ionicHistory.clearCache().then(function(){ $state.go('app.fooState') })

Note, the latter requires clearCache to return a promise. See the changes I made in this pull request and compare the implementation of clearCache that you have now with mine: https://github.com/driftyco/ionic/pull/3724

like image 175
ABCD.ca Avatar answered Sep 20 '22 13:09

ABCD.ca