How to decide what to use [Sqlite, Realm, CoreData, User-default, JSON file] to store data iOS? How to decide what to use [Sqlite, Realm, CoreData, User-default, JSON file] to store data iOS? ios ios

How to decide what to use [Sqlite, Realm, CoreData, User-default, JSON file] to store data iOS?

The most part of data storage implementation is just SQLite wrappers. The most common iOS implementations are SQLite, ORM, CoreData, Realm, Keychain.

Another part of implementations is just a simple text. For example, UserDefaults is just an XML file which you can edit using simple iOS API. But SQLite wrappers are more useful for performance reasons when you work with more than dozens of elements.

So, what about SQLite wrappers?

  1. You can use Keychain but it has several disadvantages: this is not a thread-safe, you can obtain the data only at allowed moments of application, from time to time it returns incorrect results for existed elements. This is useful for passwords, no more.
  2. CoreData has native API, but it has got memory leaks disadvantages, complicated API, this is not a thread-safe database, not a good performance.
  3. SQLite. First of all, you can use the native C library with 11KB dependency. Disadvantages: Old-style C API, raw SQL queries.
  4. SQLite Swift wrapper. In this case, you have to use third-party libraries. But well perform.
  5. SQLite ORM. This way allows using your objects without knowing who the actually map into SQLite table. Easy usage, not well performed, third-party dependency.
  6. Realm. This is really well performed and easy-to-use. But it has got a disadvantage for thread-safe. The difference between ORM and Realm is that the objects in the Realm are not stored as an obvious table, it has its own magic how to convert the data into table representation.
  7. Firebase database. I didn't use this library in the production. It has got online implementation as a primary feature. I'm not sure is it correct using a primary internal database.

What about the summary?

SQLite (Wrapper, pure C) and Realm in our test have nearly the same performance. CoreData isn't good enough.

SQLite wrapper and Realm have enough good API.

The only SQLite wrapper is really thread-safe.

Available Options to save data in iOS.

User Default: Quicky persists a small amount of data.

Codable (NSCoder): for saving the custom object.

Keychain: Small bit of data securely.

SQLite: Persist large data but require query operation.

Core Data: Object-Oriented Database.

Realm: Faster and easier database solution.

CoreData, Sqlite & Realm store plain text in-store if you don't tell it to encrypt, you can use encryption in all to make it secure.

  • Saving user-watched history [Huge amount of data, Only insert, and delete operation] Coredata & Realm should be used.

  • Saving contact numbers [Max 1000 numbers, Need fast fetch, and continuous operation] Coredata & Realm should be used.

  • Saving simple GET API request [Use for caching] Codable, Coredata & Realm should be used. The response can be used for offline storage.

Data storage and caching very important topic. Choosing way to store data can rely on whole app design.


NSURLCache: Simple option to configure cache of desired size. Works good for caching data that does not need any post processing (JSON, Image Requests).

let URLCache = NSURLCache(memoryCapacity: 4 * 1024 * 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil)NSURLCache.setSharedURLCache(URLCache)

File Cache: If you need to post process data somehow (like round corners on images, build own data structure from JSON) file cache can be an option. Images can be saved as plain files, for data objects you can use NSCoding protocol.

Database: For fully working offline mode better to use database.


NSUserDefaults: Small pieces of data. Not secure, no sync.

SQLite: Memory efficient and cross-platform. Usually not a best option. Useful when you want to share existed cross-platform database in app.

Core Data: Good option when you need complex queries and complex relations between objects. Have basic migration support and visual code editor (with code generator). Probably it should be default option unless you need: syncing or cross-platform.

YapDatabase: Faster than CoreData. Have iCloud sync.

Realm: Some article says that it is faster than CoreData and thread safe. Have a lot of options and some of them are paid (like Sync). May be a good alternative for CoreData.

Firebase: Cloud platform. Have offline mode, but paid.

CouchBase: Free Sync. No migrations needed as very flexible schema.

In a conclusion:

Simple records: UserDefaults, NSCoding
Local data storage, complicated records: CoreData (Yap, Realm)
Syncronization: CouchDB, Yap (iCloud), CoreData (iCloud-), Realm (Paid), Firebase (Paid)
Speed: Yap, Realm
Patform or Cloud Based: Firebase, Parse
Cross-platform: Realm, CouchDB, MongoDB, Firebase