Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

After searching everywhere I found there is no properer comparison between SQLite, Realm, CoreData, UserDefaults, and JSON file.

You can find pros and cons easily but it's hard to know what to use.

I know the decision is totally dependent on the requirements. But still there must be a way to make decision.

Assuming the following cases, which options are best for individual and why?

  1. Saving user watched history [Huge amount of data, Only insert, and delete operation]
  2. Saving contact numbers [Max 1000 numbers, Need fast fetch, and continuous operation]
  3. Saving simple GET API request [Use for caching]

Note: I am not talking about storing sensitive information here.

Feel free to add/update more relevant cases.

like image 716
Nick Avatar asked Jul 14 '19 04:07

Nick


People also ask

When should you use Core Data?

Here is Apple's quick overview: “Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device.” To give a bit more detail, CoreData is Apple's technology to save your structured data locally.

When should I use Core Data vs User Defaults?

Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.

Does realm use Core Data?

Though, CoreData also prevails over Realm in some ways. As you remember, the Apple framework offers a visual representation of the model in XCode. And many find the process of working with one-to-many and many-to-many relations in the Xcode model editor easier than using the LinkingObjects () function in Realm.

Does Core Data use SQLite?

Core Data can use SQLite as its persistent store, but the framework itself is not a database. Core Data is not a database. Core Data is a framework for managing an object graph.


2 Answers

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.

like image 159
Vyacheslav Avatar answered Sep 18 '22 12:09

Vyacheslav


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.

like image 34
Animesh Avatar answered Sep 17 '22 12:09

Animesh