-
Notifications
You must be signed in to change notification settings - Fork 13.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add a method to empty/flush Cached Views #2939
Comments
Ok, sorry I just realised |
I'm still playing with this.
I have the following error:
|
@gio82 I encountered the same issue, but was able to disable cache with one of below 2 ways instead of entire app from Disable cache within state provider: Disable cache with an attribute: note: purposefully omitted '<' & '>' in the above HTML as the comment section is trying to render it. |
Yeah, I ended up with the same solution but I consider this a turnaround. I hope and guess driftyco will work to fix these bugs. Thanks @acveer for your help. |
I really need something like this.
|
+1: Cached views are awesome, just need a way to clear them all on logout. |
+1 clearCache should empty all the cached views, no matter where they stand or what is the parent state. I will add some of my observations.
|
Would someone be able to reproduce this error using the nightly build? Thanks |
+1 |
Looks good to me. For me, the error no longer occurs. |
@adamdbradley okay, I spent the day at reducing my project to get a mcve that could help you guys. I'm not sure I really succeeded in this, but I did reduced it enough to demonstrate some of the issues clearly. I modified the names and all this, but it's close to the actual structure of my real project. I think it's a common pattern - there is a list of events, in each event there is a list of users, and each user has its own page. Codepen - http://codepen.io/JerryBels/pen/ogEdaK Now, you can easily see some bugs here, if I'm not mistaken. First, when selecting a user, and then coming back and selecting another, it does display the first user's page. That shouldn't be the case since it uses stateParams. Note that the bug must be somewhat linked to the multiple nested states, since I failed to reproduce it on a simple states architecture (but I'm still very young with Angular so I will let you guys figure it out). Second, when displaying the menu and switching event (clearing the cache happens there), the first time you do that it's the previous event that displays again (you can see its name in the menu). The next tries are good. I think that's not everything but it's all I could get with this simplified example. I really hope it will help you guys. Good luck ! :-) |
+1 |
+100 |
Forget my last comment, |
I don't think so. The bugs demonstrated in my codepen above still occurs. Lets say it does work sometimes. |
+100 is a workaround available? |
looks like the same bug |
Sorry everyone, but I'm not being provided with an example of it not working. I just reviewed the codepen and clear cache is working when fired from $ionicView.enter event. I'll need a detailed codepen, and detailed description showing exactly how it's not working, and a full description of what elements should be removed. |
@adamdbradley - in the codepen I provided ( http://codepen.io/JerryBels/pen/ogEdaK ) the first "switch event" doesn't work, the cache is not cleared, the same event shows up. I added a title for it to be more visible. This is a clearcache bug. Besides, even if it's not a clearcache bug, it's a huge cache bug that when you get to a user page it doesn't take its id (which is a state parameter) into account, showing the page of the user who was first opened because that page is cached ! Should I create another thread for this issue to be taken into account ? |
Details like this help me to debug. With you're app, please describe in detail how to see what you're seeing, and what I'm looking for. Note: You're not using lifecycle events, so controller logic is only going to fire once, which is what you're seeing in your profileCtrl. But I think the biggest issue is that the |
Okay @adamdbradley, sorry :) So, steps 1 to 4 are good...
To be completely honest, I have much more problems with cache on my real app but have a hard time minimizing it to a mcve while keeping the bugs, so I stopped when I observed this bug. Maybe solving it will solve the rest. As of the abstract state, I thought I was forced to use it, since the doc ( https://github.com/angular-ui/ui-router/wiki/Nested-States-%26-Nested-Views#abstract-states ) says it's usefull to fulfill my use case ( "insert a template with its own ui-view(s) that its child states will populate", $scope and resolved dependencies inheritance...) but it looks like the same state without being stated as abstract does exactly the same thing... And then the cache does work well. Did I misunderstood what abstract is good for ? Also, when disabling the cache completely, it works even when stated as abstract without any issue. |
@adamdbradley following your message about abstract not being usefull, I just took every abstract state off, as well as users concept. Here is the minimized codepen : http://codepen.io/anon/pen/Joxdeb Now the clear cache fails all the time, which demonstrate much better the issue (I still feel it's linked to nested states). To reproduce simply :
I hope that helps to resolve the issue ! |
+1.
I have to set Would be great if Version: Ionic 1.0 RC1 |
@buunguyen I fixed it by creating a state that only check if the user is
The main problem with History stack is like a array But there's differences by using This is related to http://forum.ionicframework.com/t/clearcache-possible-bug/16532 |
@felquis can you post a Codepen example, please? |
@felquis thank you for the tips. What I actually did was moving the |
Thanks for the commit guys, I will test it and feedback whenever it's included into ionic.bundle.js. |
+1 here does not work |
$ionicHistory.clearCache() did not work for me either. Timeouting a call to $ionicHistory.clearCache() resolved the problem:
|
Tested, still get cached views. |
Yep, I'm also seeing this on v1.1.0. Wrapping it in a $timeout doesn't work either, nor does using the promise version. @jordidiaz, how are you clearing this for each view? The docs don't mention that you can pass in a state? |
To clear view data (for example, has mentioned by @eroh92) I listen to a 'logout' event and clear the data in the event callback function. It's a manual work but if I don't do that the previous user's data is shown to the just logged user. If someone has a better method, I'll be very thankful. |
i also have this issue in my signInCtrl:
its working for me when i add the function in the doSignin FUNC |
and this also help me in my signoutCtrl authService.doSignout().then(function(result){ |
Thanks to @mousenine , wrapping with a timeout works for me too (seems it just need to be on the same digest, so no need to set a number of ms).
|
This one works for me $ionicHistory.clearCache().then(function() {
$state.go('...');
}); |
None of the above solutions worked for me and I tried all of them over a span of a few hours. :( |
@sargismarkosyan gave the solution indeed, I found out the same. Waiting for the promise to resolve before navigating to the next state works perfectly. |
I apologise for jumping in on a closed topic but here's something for those who this is not working for. Have you considered that the issue might be that you are storing certain data in a global variable or within a service/factory rather than the issue being that Ionic is not clearing the cache? This happened to me. Like everyone else, I was trying to blame it on Ionic and the clearing of cached views not working but the issue was because I wasn't clearing a global variable that was storing my data. That, said, to ensure the clearing of cache works in Ionic, from my experience like others above, putting the clear cache function in a $timeout is the only way I was able to get it to work:
|
Something to notice is that since 1.1.0 release, clearCache() returns a promise called when cache is flushed. Didn't test it myself, but it seems to be the clean way to do it. |
By docs it must return promise, but it doesn't. So, almost year is gone and there is no answer to question "How to clear cache". |
@sergeykuzmich there have been a number of answers to the problem, and yes, the function does return a promise: There's a high chance this service is being used incorrectly, or the issue is coming from somewhere else (including user code). However, since this comment has gotten a lot of questions over the last year, it's clear this function is causing some confusion and/or is buggy. To help us fix this, can anyone confirm this is still happening in a recent version of Ionic? I'm testing some of the code pens and not really seeing any issues with nightly/1.1.1. Any other recent test cases will help us figure out what's going on. |
Okay, noticing a few things here. First, you need to call $scope.$on('$ionicView.enter', function() {
$timeout(function() {
$ionicHistory.clearCache();
});
}); We will look to a better solution. Some WIP: https://github.com/driftyco/ionic/tree/2939-view-events |
Just landed in master: 06ef682 With this fix, this code will work to clear any cached views behind the view that just entered: $scope.$on('$ionicView.enter', function() {
$ionicHistory.clearCache()
}); Will work. Mind giving master a try and letting me know if it resolves your issues? |
@mlynch thanks for your response I've got a little different use case of clear cache. I've got feed of some data, by clicking to each item of data you see description of item and there is 'delete item' button. After pressing it item must be removed and user goes to feed screen and I need to refresh feed screen (reset cached view) and completely reload feed, because deleting one item can delete others connected items. So I need to clear cache after some action. I've tried to use $ionicView.afterLeave event but nothing is changed, also I tried to use $timeout function - it is better but it did not clear cache for view I go and clear cache for others. So,
doesn't work, and
works only if I have middle step such as
and in the 'middle' call I'm working with private business app and not sure can I show it in CodePen or not. I've tried to repeat behavior in CodePen to show what I want to get. |
could u please provide codepen example without using factory, Thanks |
The code provided by @mlynch is working for me: $scope.$on('$ionicView.enter', function() {
$ionicHistory.clearCache()
}); it must be placed after the logout, in my case in the welcome controller |
This is what I use during logout // After logging out, ensure hitting back button from login screen doesn't return to the account settings page
$ionicHistory.nextViewOptions({
historyRoot: true,
disableBack: true,
});
$ionicHistory.clearCache().then(function () {
$ionicHistory.clearHistory();
}); Works for me |
i am using bellow code but not entering in block to show alert. |
This woked for me $timeout(function(){ $state.go('app.login'); |
can any body solve my issue ? |
This worked for 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(): So, let's go deeper. The code of $ionicNavView.clearCache which is used in the line above "instance.clearCache(stateIds)" is: `
}; 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: UPDATE: Just to make it crystal clear, i want to point out where the bug itself is (if we can call it bug), maybe can be handy for devs:
What the whole function does is basically:
I wont dig deeper into this but debugging it i could see that the elements gotten from Besides there's another strange behavior, because when i was debugging it i saw when the var viewElements array was filled up with 2 elements, and these 2 elements were from the same state, one resolved to 'CACHED' another resolver to 'ACTIVE', even resolving to the 2 types used in the if conditions the cache was not cleared at all. I personally think that this is somekind wrong implemented or is wide used wrongly. The fact is that there's a lot of people cracking their heads on this and devs don't even give this simple explanation. |
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out. |
Type: feat
Platform: all
I have an app that needs a login to see personal informations.
Since ionic beta 14 and the introduction of Cached Views I have big trouble when a user logoff and login with a different user.
The new user after the login still see the cached views of the previous user.
At the moment it seems that the only solution to avoid this is to disable everywhere the cached views (
$ionicConfigProvider.views.maxCache(0);
).It would be very useful to have a ionic.flushCachedViews() method to clear all the cached views.
I would call this when a user logoff.
The text was updated successfully, but these errors were encountered: