traglen
traglen

Reputation: 39

Is UserDefaults a bad practice to cache data?

As a newbie on iOS, I am devoloping a small app. There are 5 different pages and 4 of them has an imageView for user's profile photo, I am storing and downloading profile photo in Firebase.

Is it a bad practice to store the profile photo on userdefaults to not the download for each page that has imageView? I also have to store user's name as a string an a date (date of the user opened my app for the first time)

I know I got options like NSCache or Core data but UserDefault looks 10x time easier than them. I read on the internet that UserDefaults is only good for small amount of data, otherwise it will slow down my app performance, but they never mention how small.

So an image that has max 500kb size and a string with a date would be a performance problem? Is UserDefaults only for saving things like settings or a boolean for onboarding etc.?

Upvotes: 1

Views: 801

Answers (1)

lorem ipsum
lorem ipsum

Reputation: 29242

Yes it is bad practice (and inefficient), UserDefaults is for small stuff like settings and preferences, small unimportant and low vulnerability stuff.

https://developer.apple.com/documentation/foundation/userdefaults

KeyChain is for small stuff that needs to be secure such as user data, tokens, etc.

https://developer.apple.com/documentation/security/keychain_services

Apple has provided other locations such as for "caching"

cachesDirectory

https://developer.apple.com/documentation/foundation/filemanager/searchpathdirectory

Upvotes: 1

Related Questions