Reputation: 598
I'm struggling to think of a good solution here. So, in my app, I have a UITableView that loads user data, and each cell has a profile image UIImageView. So, every single time it's looping through the cells, it's individually downloading the profile pic for each user. For THIS reason, I started using NSCache to store the profile pics. This worked tremendously, and now the lag is gone.
However, what if someone changes their profile pic? The profile pics are uploaded in backend one per user. I use User ID to reference these profile pics. If someone changes their profile pic, it will just load the image I have in the cache, and not their new image. So, I want to have the entire cache on the IOS device cleared like once every 3 hours, or so. That will cure everything. How do you clear the entire NSCache at intervals?
TL;DR:
How would I clear the whole NSCache for an app at time intervals? (example: have the app cache cleared every 3 hours)
Upvotes: 2
Views: 2124
Reputation: 1836
You could use the app delegate methods and implement a check every time the user enters foreground of the app. You could keep a variable that holds the last time the user opened the app and if the current time is 3 hours past then, clear the cache.
As stated in the comments, most users will not have 3 hour sessions.
On a side note: Most users may understand images not updating immediately, For instance, I know twitter takes awhile to update. In my app, I leave the cache alone and re-download all images any time the app is completely quit. If you compress the images before putting them on the server this won't hurt data usage to bad.
Upvotes: 3