简体   繁体   English

UserDefaults 是缓存数据的坏习惯吗?

[英]Is UserDefaults a bad practice to cache data?

As a newbie on iOS, I am devoloping a small app.作为 iOS 的新手,我正在开发一个小应用程序。 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.有 5 个不同的页面,其中 4 个有一个用于用户个人资料照片的 imageView,我正在 Firebase 中存储和下载个人资料照片。

Is it a bad practice to store the profile photo on userdefaults to not the download for each page that has imageView?将个人资料照片存储在 userdefaults 上而不是为每个具有 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.我知道我有像 NSCache 或核心数据这样的选项,但 UserDefault 看起来比它们容易 10 倍。 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.我在互联网上读到 UserDefaults 仅适用于少量数据,否则会降低我的应用程序性能,但他们从未提及有多小。

So an image that has max 500kb size and a string with a date would be a performance problem?那么一个最大 500kb 大小的图像和一个带有日期的字符串会是一个性能问题吗? Is UserDefaults only for saving things like settings or a boolean for onboarding etc.? UserDefaults 是否仅用于保存设置或用于入职等的布尔值?

Yes it is bad practice (and inefficient), UserDefaults is for small stuff like settings and preferences, small unimportant and low vulnerability stuff.是的,这是不好的做法(而且效率低下),UserDefaults 用于设置和首选项等小东西,小的不重要和低漏洞的东西。

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

KeyChain is for small stuff that needs to be secure such as user data, tokens, etc. KeyChain 用于需要安全的小东西,例如用户数据、令牌等。

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

Apple has provided other locations such as for "caching" Apple 提供了其他位置,例如“缓存”

cachesDirectory

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM