Data storage is one of the key tasks of a mobile application, affecting performance, security, and user experience. In this article, we will cover SQLite, Room, Core Data, Realm, Firebase Firestore, SharedPreferences, DataStore, Keystore and Keychain, as well as serialization formats (JSON, Protobuf) and libraries (Gson, Moshi, kotlinx.serialization). This guide will help beginner developers choose the right data storage solution. Learn more in the official Android data storage guide.
Key Takeaways
Relational databases are used for storing structured data with relationships between entities.
SQLite is an embedded relational database that works on any mobile platform. It requires no separate server, and data is stored in a single file. SQLite is the foundation for Room (Android) and optionally for Core Data (iOS). Libraries: android.database.sqlite (Android), FMDB/Core Data (iOS), sqflite (Flutter).
Room is the official Android Architecture Components library for working with SQLite. It provides an ORM layer: Entity (table), DAO (queries), Database (entry point). Room checks SQL queries at compile time, supports coroutines and Flow, and automatically migrates schemas.
@Entity data class User( @PrimaryKey val id: Int, val name: String, val email: String ) @Dao interface UserDao { @Query("SELECT * FROM user WHERE id = :id") suspend fun getUser(id: Int): User? }
Core Data is Apple's framework for managing an object graph. It is not a pure database — it is a persistence layer on top of SQLite, XML, or binary storage. Core Data manages objects (NSManagedObject), relationships, schema versioning, undo/redo, and multithreading via NSManagedObjectContext.
Realm is an alternative to SQLite, designed as a high-performance mobile database. Realm is faster than SQLite for reads and writes, supports reactive notifications (Live Objects), on-the-fly encryption, and cross-platform use (Android, iOS, Flutter, React Native). Disadvantages: increased APK/IPA size and higher memory usage.
For storing small amounts of data (settings, tokens, cache), simple key-value stores are used.
SharedPreferences is a legacy key-value store in Android. Cons: synchronous access (blocks UI on read), lack of typing, ANR risk when loading large files. NOT recommended for new projects.
DataStore is a modern replacement for SharedPreferences from Google. It works asynchronously via Kotlin Coroutines and Flow, supports Preferences DataStore (key-value) and Proto DataStore (typed data via Protobuf). DataStore is safe for the UI thread and supports transactions.
UserDefaults is the standard storage for small settings in iOS. It supports primitive types, strings, dates, Data. UserDefaults is synchronous, so it is not suitable for large data volumes. Register default values via register(defaults:).
For data storage in applications requiring synchronization across devices, Firebase cloud databases are used.
Firebase Firestore is a NoSQL document database with real-time subscriptions, automatic scaling, powerful queries, and built-in offline mode. Data is organized into collections and documents (JSON-like structures). Firestore supports transactions and batch operations. Ideal for chats, feeds, and social apps.
Firebase Realtime Database is an older Firebase NoSQL database that stores data as a single JSON tree. Simpler than Firestore but less flexible in queries and scaling. Recommended for simple real-time applications (online status, simple chats).
| Feature | Cloud Firestore | Firebase Realtime DB |
|---|---|---|
| Data Model | Collections → Documents | JSON tree |
| Queries | Complex (filtering, sorting, limits) | Simple (by key) |
| Scaling | Automatic | Manual sharding |
| Offline Mode | Yes (persistent cache) | Yes |
| Pricing | Per read/write operations | Per traffic + storage |
Storing tokens, passwords, and encryption keys requires special protected storage.
Android Keystore is a system storage for cryptographic keys. Keys are stored in a hardware environment (TEE — Trusted Execution Environment), inaccessible to apps and the OS. It supports key generation, encryption, and signing. Android 9+ includes StrongBox Keymaster (dedicated chip).
Keychain Services is a secure iOS storage for passwords, keys, and certificates. Data is encrypted using the hardware Secure Enclave. Keychain supports accessibility levels: Always, WhenUnlocked, WhenUnlockedThisDeviceOnly, AfterFirstUnlock.
EncryptedSharedPreferences is a wrapper around SharedPreferences with key and value encryption using AES256-GCM and HKDF. Implemented in the AndroidX Security library. Recommended for storing tokens and sensitive settings.
At IT Sectr, we use Android Keystore + EncryptedSharedPreferences for storing refresh tokens and biometric-based keys. On iOS, we use Keychain with the kSecAttrAccessibleWhenUnlockedThisDeviceOnly level for maximum security.
For file storage (images, videos, documents), mobile platforms provide several storage types.
Android distinguishes between Internal Storage (/data/data/package/ — private storage) and External Storage (SD card or emulated partition). Cache Directory is temporary storage that the system can clear. Scoped Storage (Android 10+) restricts access to External Storage — apps see only their own files or files via MediaStore/SAF.
iOS provides Documents Directory (iCloud backup, persistent data), Library/Caches (temporary files, not backed up), and Tmp Directory (temporary, can be cleared). Use NSFileManager to work with the file system. iOS 11+ supports File Provider for cloud files.
Serialization converts objects into a format for transmission or storage. Deserialization is the reverse process.
JSON (JavaScript Object Notation) is the most popular format: readable, flexible, supported by all platforms. XML is stricter, used in legacy systems. Protobuf (Protocol Buffers) is a binary format from Google: 3-10x more compact than JSON, faster to parse, with a strict schema. Used for high-load systems and microservices.
JSONSerialization is the built-in API for working with JSON. Codable (Encodable/Decodable) is a modern Swift protocol for automatic serialization to JSON, Property List, and other formats.
struct User: Codable { let id: Int let name: String let email: String } let user = User(id: 1, name: "Alice", email: "alice@example.com") let encoder = JSONEncoder() let jsonData = try encoder.encode(user)
For Flutter and React Native, there are efficient storage libraries of their own.
Hive is a lightweight, fast key-value store for Flutter. It requires no native code (pure Dart), supports typing via TypeAdapter, and works in isolates. Ideal for cache, settings, and small data volumes.
ObjectBox is a high-performance embedded database for Flutter, Android, and iOS. It is 10x faster than SQLite, uses a flat file structure for minimal overhead. Supports relationships, queries, and reactive subscriptions (ObjectBox Dart).
Drift is a reactive SQLite library for Flutter and Dart. It provides type-safe queries, migrations, DAO functions, and Stream support (reactive updates). Drift is the primary choice for Flutter projects requiring SQLite.
Frequently Asked Questions
The choice depends on the platform: Room (Android) is the official library based on SQLite with compile-time query checking. Core Data (iOS) is Apple's framework with an object graph. For cross-platform projects, Realm or SQLite via ORM works well.
SharedPreferences is an old implementation with synchronous access and ANR risk. DataStore (Jetpack) is a modern replacement based on Kotlin Coroutines and Flow, supporting asynchronicity, typing, and error protection.
Android: EncryptedSharedPreferences or Android Keystore. iOS: Keychain Services with WhenUnlockedThisDeviceOnly accessibility. For critical data, use Secure Enclave (iOS) and hardware Keystore (Android) with biometrics.
Cloud Firestore is a NoSQL database from Firebase with real-time synchronization, offline mode, and automatic scaling. Suitable for chats, news feeds, and projects that need synchronization between devices.
Flutter: Hive (fast key-value), Floor (SQLite ORM), Drift (Moor, reactive SQLite), ObjectBox (high-performance embedded DB). React Native: Realm, WatermelonDB (lazy loading) and AsyncStorage (analog of SharedPreferences).
Summary
We will develop a mobile application turnkey
IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.