Search code examples
ioscore-datansuserdefaultskeychain

breaking down use cases of different storage methods in iOS apps


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

  1. an independent MySQL DB
  2. amazon s3
  3. core data
  4. nsuserdefaults
  5. 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.


Solution

  • 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.