How to periodically clear NSCache in swift? - ios

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)

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.

Related

Downloading content on app's first launch

Working on an app which is relying heavily to download content (images and sound files) from the server while the app is launched for the first time. It may take awhile depending on the connection speed. Just wondering if that delay may cause my app to be rejected by AppStore review team? Though, be assured that I will display proper user feedback view to show progress indicator while the content is being downloaded. Kindly, share your valuable insights. Thanks
What you should do is show a first view in which you show the status of the download. Once the download is complete yo move to the firs controller of your app. You could save a variable to know that the initial download has already been done, and int future openings not go
through the downloading view.

iOS limit usage of a feature

Problem
When A user downloads my application, they'll have the ability to use a certain feature. Say, 5 times. After them times are up, they'll require the iAP to continue using it. However, I have a few problems.
I want to make it simple, and just store it in the userDefaults, or keychain. However, if they re-download the app will they then have access to the feature another 5 times?
And I don't want to have to include external information from a data-base with user logins etc.
Solution
Being able to check if this particular phone has used the application before, if not. They have 5 goes stored in their defaults. However, if they haven't where to go from there?
1- At anytime the user may restore his device and reconstruct the keychain and use 5 times more.
2- You can set a server for user management.
I guess first option is acceptable and easier than second. Not much people resets his device's data just 5 more rights in some game.
Check NSUserDefualts if you have saved their usage counter. If you haven't, save it as 1, otherwise increment it.
If you've saved it before, and the increment is at 5, tell them that free trial is up.
Technology-wise - if they delete the app, I believe you'll lose your NSUserDefaults as you can see here. What about using iCloud? Is that too much? You could store that trial is up. But yes, internet connection would be necessary for that operation.
If your app is really good and users will want to use it then they won't think about deleting and re-downloading. If they won't see value in the iAP, they'll try to "hack" it. That's a philosophical answer...

iOS Assets Library: permanent unique key for photos(identify photos between multiple runs)?

Say I have an app which allows user to pick some images from assets library.
When running for the 1st time, user picks some photo, at the 2nd run, when user wants to pick photos, I'd like to set the photos user picked last time as already selected.
How do I identify photos between multiple runs of my app?
iOS apps usually don't get terminated, but get sent to background - state will be unchanged when getting back to foreground
For case your app get's terminated anyway, you can store data in NSUserDefaults, CoreData, the file system or the keychain. In your case it sounds like NSUserDefaults would be a good choice. Don't forget to restore last states on app start.

how often should i refresh my cache?

I'm writing the cache for an iOS app right now. I'm wondering how often i should refresh my cache. to give a little background, it's a weight loss app. what i'm working on right now, specifically, is a list of recipes. I pull the list of recipes from the server and display it in a table view. Obviously, i was going to make the list refresh when you pull down on the view controller. My question is, should i have it refresh the list every x number of days? or should it refresh every time the app is reopened? the list isnt expected to change more than once every 1 - 2 weeks, if that often. the user can also add their own recipes, so it could potentially change more often than that. is there a specific industry standard or protocol to follow for how often the cache should be refreshed?
Why don't you hook up your app with a Push Notification Service ? so incase there is a new recipe you could dispatch a push notification and when the user opens the app refresh your cache. That way you only refresh your cache when needed and also aids in better user experience. Here's a good tutorial on setting up a push notification service

Keep track of time since last attempt of ______

I have a game where users can do a certain activity once per hour. How can I make sure it's been an hour since the last time they attempted something without them just changing their devices current time in settings?
Also, if I wanted to prevent the user from just deleting the app and re-installing it so they could constantly keep trying without having to wait to full hour is there any way I can store data on the device even after an app delete or would that have to be a server thing?
If I don't have a server can anyone think of a clever way to do this via Free in-app purchases or something?
The only way to persist data in a way that survives app reinstalls is to save it to the keychain. This works, because keychain data may be shared across multiple applications; the rest of your application's data is removed on uninstall.
If you need a reliable way to tell the current time, the device must be connected to the internet. In this case you would be able to check the current time using one of the time services through the NTP.
That sounds like exactly the sort of task you would need a server for.
When the user wants to perform this limited action, have them ask the server for permission. You can log the user's ID and request time, decide if they can execute the action, then return a small success/failure message. Works if they change their clock, works if they log in from a different device, works if they wipe the device data.

Resources