Is UserDefaults a bad practice to cache data? [closed] - ios

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 months ago.
Improve this question
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.?

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

Related

When offline, how to manage data? IOS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I would like to know what are the best practices regarding, Modeling data when no network connection available, if the app you are building is cloud computing based, but still you want to be able to have basic functionality and I guess some persistent data?
PD: I am kind of new to IOS development
UserDefaults is okay for small bits of data that don't change often, but as it has to rewrite the entire user defaults dataset to a file each time a change it made, it is not robust enough for anything of volume or with frequent changes. For that you would want CoreData or a third party open source solution like Realm.io.
You can try to using the 'cache' where you store temporary data.
One way to achieve this is NSUserDefaults where you set a variable (let's say users profile photo) and when the user opens his app again, the image will be loaded even if there is no internet connection since its cached. Hope this helps!

iOS App Preloaded Data Guidance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm working on an app with quite a few images and general information about them. Are there any general guidelines about when it is a good idea to just bundle the data with your app and when it should just be downloaded on first run? When you should use Core Data and when just keyed archiving is sufficient? Or is there a better solution I haven't even considered?
I imagine that the data will be updated from time to time, but not frequently. I'd like the app to be able to download updates.
Kind of a vague question, and I apologize for that.
It depends on whether the initial data (images & information) is important and always the same.
If you wish to have it dynamically changed to whatever is updated on the server then you shouldn't bundle it within the app. On the other hand, if the initial data is trivial and you can just include it in the app.
Now if you wish to store the initial data locally in the app, given that the data is just images and theirs information, I would recommend to just use keyed archiving to keep things nice and simple.

How reliable are NSUserDefaults? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Not so long ago I found out about NSUserDefaults and thought: If I can just keep the information my application needs as NSUserDefaults, why bother creating a database? These are the variables my application needs:
Username (of type String)
Coins earned (of type Int16)
Bucks bought (of type Int16)
Items bought (of type [String])
How suitable are NSUserDefaults as an alternative to using a database? If I do use NSUserDefaults instead of a database, would it be reasonable to list all the "buyable" items as an Enum?
The NSUserDefaults documentation clearly states it's primary function is to store user preferences:
The defaults system allows an application to customize its behavior to match a user’s preferences.
Of course there's nothing to stop you from using it otherwise, it's a very cheap and reliable way of reading and writing data, but it's worth knowing that it isn't designed to store application constants and variables.
Furthermore everything you write to it will be available to read in plain text if the user looks into his application container (and therefore easily editable). Therefore you probably don't want to be using it for anything that the user can change to get an advantage in your game.
You might want to look into NSKeyedArchiver for storing custom objects that could contain your data. This will allow far greater flexibility, and you can encode your data in a non-plain text format.
NSUserDefaults is used mostly for preferences not for sensitive data.

How and When to Store Settings & User Data in an iOS App? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I developed an iPhone game in Swift using Xcode. It needs to store settings as well as user data. How do I get the app's settings and data to save when it is restarted or quit?
As mentioned in comments, use NSUserDefaults for settings. For "large files" it's more likely you mean "user data". In that case, you can use Core Data or store your own NSCoding-compliant data model via NSKeyedArchiver (and unarchived via NSKeyedUnarchiver) or their ...Secure... alternatives to write to your app's documents.
Rather than save only when the application's state changes, you should probably persist small changes as they're made (this is basically what NSUserDefaults does) or as some logical group of changes are made (what constitutes a "logical group" depends entirely on the nature of the data and your app and is therefore up to you).
So: Identify the "settings" and store them the right way (in NSUserDefaults). Then identify your user's game data and store that the right way (in some sort of data file).

Storing Hundreds of Images [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to determine what would be the best route to go for an app idea I have in regards to using a back-end service or not.
The app will not require any sign up / log in steps
The concept does have a part of taking a photo. For example, a photo could be taken every day, or multiple photos per day, equaling 365 + photos.
I'd like for the user to be able to view these photos at any time, and possibly export them all at once in some form (if that is possible) at any time.
What's the best way of doing this to have a good experience in the app?
I supposed you'd define a good experience in terms of photo storage by being quick. You really don't have many options. You could either write 365+ photos into your documents directory or write them into a CoreData store. I'd probably opt for the CoreData store, because I have a feeling you're going to have some metadata associated with the image.
Really though, this question is a bit too specific to your situation. See also Best way to store images in iOS

Resources