AndroidME AndroidME - 3 months ago 16
iOS Question

How can i store my data locally using iOS + Swift?

I am new for Swift iOS development. I want to store my data (name, image_path) locally while my app is not able to upload it (no internet connection etc). I have started my work before two days but still not getting any good result for my problem. I have also worked on Realm and SQlite database, but still not getting proper success. Please assist me, provide me proper flow for storing data (name, image_path) into local database.
Thank You.

Answer

There are basically five options for local storage on iOS:

  • Core Data
  • 3rd Party DB (Realm etc)
  • NSKeyedArchiver/NSCoding
  • NSUserDefaults
  • Keychain Services

1 (Core Data) is best when you want to persist large amounts of data in a structured format, example: Your contacts/address book, a music library.

2 (Realm / 3rd Party DB) is useful in similar scenarios to 1. Core Data can be a little dense/turgid to use, which is why 3rd party abstractions/alternatives exist.

3 (NSKeyedArchiver) is quite an interesting option, it effectively lets you 'freeze' objects (that conform to the NSCoding protocol) and store them to disk. It's great if your data is represented as a custom object model.

4 (NSUserDefaults) is really only used to store microdata/state. Think things like: a high score integer, position in a view hierarchy, nickname in a game.

5 (Keychain Services) is only used to securely store credentials (username/passwords)

If you want to store images then really Core Data or NSKeyArchives of the UIView objects is probably best.

See here for more info: NSCoding, Core Data, Keychain.

Comments