i am trying to work on nsuserdefaults but few things are confusing me in apple reference and setting guide they says
Preferences are pieces of information that you store persistently and
use to configure your app. Apps often expose preferences to users so
that they can customize the appearance and behavior of the app. Most
preferences are stored locally using the Cocoa preferences
system—known as the user defaults system. Apps can also store
preferences in a user’s iCloud account using the key-value store.
The user defaults system and key-value store are both designed for
storing simple data types—strings, numbers, dates, Boolean values,
URLs, data objects, and so forth—in a property list. The use of a
property list also means you can organize your preference data using
array and dictionary types. It is also possible to store other objects
in a property list by encoding them into an NSData object first.
but what is users defaults system and further on in this guide they say users defaults database... if they are talking about database then why here they wrote use of plist?
similar question but not helping me
thanks in advance.
NSUserDefaults is a key value store for saving preferences. It works very much like an NSDictionary where you insert an object for a key, and pull it out.
Since the object needs to be saved to disk, only plist serializable objects work, unless you turn them into NSData first.
It is not the type of database you can run SQL queries on.
Related
I have an app that stores lists of strings.
I currently have a CoreData entity for a list and for a user; a user has a relationship to many lists. A user can create more lists.
But I would like to include some default lists. Where should I store these default lists?
The options that come to mind are:
Prepopulate CoreData with some lists
Store default lists in UserDefaults
Store default lists in plist file
Explicitly create default lists in code where they are needed
What is the best way to store a default array of strings for an iOS app?
IMO there is no one-answer to your question an these really depends on your use case.
In general, if your user shall have any interaction with the default lists (manipulating, hide, delete) i would put them in your CoreData.
I assume since you have an entity for the user, you allow multiple different users to use the app on the same device? So if you would store them in the userDefaults you would also have to keep track off the modifications for each user. In general the userDefaults should only be used for "small" data, cf. doc
The defaults system allows an app to customize its behavior to match a
user’s preferences. For example, you can allow users to specify their
preferred units of measurement or media playback speed.
I am taking development courses for ios and I was wondering if I wanted to create a translation app would i use a array list to do so? As example code is:-
var dictionary = [“talofa”: “hello’, “faafetai”: “thank you”]
print(dictionary[“talofa”])
it shows up in the logs as “hello” but there has to be an easier way to do translations otherwise I would be fitting a whole language in a array list?
I also read online that people have been using third party services like google to make a translation app but my language is not on google (Hawaiian) what do I do?
First of all, what you are using in your example, in swift/objc it is called a dictionary.
Secondly, for such a huge amount of data, I recommend you use some sort of persistent storage. You can use plain text to store the dictionary (like creating a .plist file), but being iOS I would recommend setting up coredata.
CoreData will allow you to store the information on the device, and access it through a data Model.
Here you can find an example on storing in a file.
Here you can find an example on storing in CoreData.
I personally recommend using coredata for such a large quantity of data. Plist files are more suitable for storing low information quantities (like saving some credentials, some settings, etc).
You need to use DB for this. You can update it from your server when user will have connection, so you don't need to re-submit your app when you will update your vocabulary.
You can use CoreData as #Alex Bartiş told you or you can try another one which becomes popular: Realm
I'm learning to build iOS swift apps, and I'd appreciate guidance on what storage options to use for different parts of my app. Perhaps examples of how other professional apps architect their storage would be helpful too.
To be specific, I see my options as
an independent MySQL DB
amazon s3
core data
nsuserdefaults
keychain
The app allows users to see/stream/download/upload videos and photos to their account/app as well as do all the normal voting, customizing user preferences, etc. This is an arbitrary app example though. A social media app storage would also be a good case study for me.
Given the variety of functionality to implement, I'm curious as to the best practices for storage architecture in maintaining sessions, persistence, and security.
Right now all I've implemented is having the user create an account and login by doing http requests to the MySQL DB. And i upload and download media from my s3. Each time user data is pulled anew from the DB.
Should i cache/archive stuff into coredata to make it faster for the user?
If a user wants the app to "remember me", where is that data stored?
Instead of straight http calls for logging in, should i do something with keychain? (keychains are the only thing i haven't implemented yet in that list. The rest I've messed with independently)
I've also heard nsuserdefaults is only to be used for user preferences.
Thanks for all advise.
Yes You should use core data for better performance.
Core Data would be a much better tool for the job:
->No mismatch between cache index file and actual data stored;
->Trivial querying;
->Nice and easy object oriented code.
NSUserDefaults
If you want to add Remember me option you have to use NSUserDefaults. It will store in plist file. For more information you should go through this.
KeyChain
Sensitive data like passwords and keys should be stored in the Keychain. Apple's Keychain Services Programming Guide states that a "keychain is an encrypted container that holds passwords for multiple applications and secure services. Keychains are secure storage containers, which means that when the keychain is locked, no one can access its protected contents". Moreover, in iOS, each application only has access to its own keychain items.
You interact with the Keychain by passing in a dictionary of key-value pairs that you want to find or create. Each key represents a search option or an attribute of the item in the keychain.
I am trying to add arrays,dictionaries via NSUserdefaults.
The physical path is rootOfMyApplication/Library/Preferences/bundleidentifier.appName.plist where I can see data that I stored via NSUserDefaults while testing on simulator.
Suppose I give simulator hash folder to a friend without source code, will it be possible for him to identify the keys that I have used while storing in NSUserdefaults?
There are other keys in the plist ,How can we differentiate between the ones that are stored via NSuserdefaults?
My experience looking at the NSUserDefaults plist file shows that keys that start with the following are from Apple:
Apple...
NS...
Web...
As long as you avoid those prefixes you should be able to find all of the app keys.
I always try to use a specific prefix to my keys to be safe but you don't have to.
My application uses the PersistentStore to store data. The keys of the PersistentStore are created at runtime, so there's no way to know all the keys. I'd like to know if there's a way to retrieve or list the persistent store keys used by my application. I want to put them on a ObjectChoiceField and select the one I want to work with.
My application creates a name as a string, and then turns the string into a long value used as a key. I'm currently thinking to store the names of the PersistentStore on a single RMS, and with it retrieve the keys. But I would like to keep this option as a last resort.
The keys of the PS are created at runtime
You really want to use well-known keys rather than generating them at runtime. The persistent store is shared across all apps on the device, and I'm not aware of any way to list all the keys.
You could store all the keys as they are generated, which may be what you are suggesting as your last-resort. You would still need to keep one well-known key, and use that well-known key to store a Vector or array of all the other keys that have been generated.