Introduction
When building iOS apps, storing and managing data locally is a common requirement. Developers have multiple options — from low-level database wrappers like SQLite.swift, to Apple’s own Core Data framework, and modern third-party solutions like Realm.
It is good to have the basic understanding of these three iOS storage mechanism. Each option comes with its own architecture, performance profile, and learning curve. In this article, lets look into the basic concepts, key differences, use cases and some of the interview question-answers on this.
1. SQLite.swift — Lightweight SQL Wrapper
What it is:
- SQLite.swift is a type-safe Swift wrapper for SQLite, the lightweight relational database.
- It allows direct control over SQL tables, queries, and transactions.
Key Features:
- Full SQL syntax control.
- Lightweight, minimal abstraction.
- Requires manual schema creation & migrations.
Best for:
- Apps requiring fine-tuned SQL queries.
- Developers with SQL experience.
2. Core Data — Apple’s Object Graph & Persistence Framework
What it is:
- Core Data is Apple’s native framework for managing object graphs and persisting data.
- Works as an ORM (Object-Relational Mapping) layer, abstracting away SQL.
Key Features:
- Tight integration with Xcode & iOS frameworks.
- Built-in undo/redo, change tracking, and lightweight migrations.
- Strong type safety via Xcode Data Model editor.
Best for:
- Complex object relationships.
- Apps requiring undo/redo or iCloud sync.
3. Realm — Modern Cross-Platform Database
What it is:
- Realm is a high-performance, object-oriented database designed for mobile apps.
- Cross-platform (iOS, Android, etc.) with reactive data updates.
Key Features:
- Simple model definition with
Object
subclasses. - No migrations for basic schema changes.
- Live, reactive objects — changes in DB instantly reflect in UI.
Best for:
- Apps needing high performance & real-time updates.
- Multi-platform development with shared models.
4. Quick Comparison Table
Feature / DB | SQLite.swift | Core Data | Realm |
---|---|---|---|
Type | SQL wrapper | ORM framework | Object database |
Learning Curve | Medium (SQL knowledge needed) | Medium-High | Low |
Performance | High for raw queries | Optimized for Apple ecosystem | Very high for large datasets |
Migrations | Manual | Lightweight migrations supported | Mostly automatic |
Relationships | Manual joins | Built-in | Built-in |
Cross-platform | No | No | Yes |
Best For | SQL-heavy apps | iOS/macOS-only complex apps | High-performance, real-time apps |
Code Size for CRUD | Verbose | Medium | Very concise |
5. Related interview questions & answers:
- How does Core Data differ from a traditional database like SQLite?
– Core Data is an object graph manager, not just a database.
– It provides tracking, undo/redo, lazy loading, validation, and more.
– Core Data can use SQLite as its persistence store but hides the schema. - How would you insert a large number of records efficiently in CoreData?
– Use a background context
– Disable undo manager temporarily
– Save in batches (e.g. every 100 records) - What are some performance best practices in Core Data?
– Use faulting and batch fetching
– Avoid fetching all data at once - Which solution scales better when dealing with 1M+ records?
– Realm often scales better due to its lazy loading and zero-copy architecture.
– SQLite.swift can scale if queries are optimized, indexes are set, and pagination is used.
– Core Data can handle large datasets, but without proper fetch limits and batch sizes, memory usage can spike. - What’s the impact of thread safety in each technology?
– SQLite.swift: You must manage thread safety manually (open connections on the right queue).
– Core Data: Contexts are thread-bound — must use background contexts for heavy operations.
– Realm: Thread-safe references are needed to pass objects between threads, but generally easier to handle than Core Data. - Which database choice will make app migration to Android easier?
– Realm → Easiest, since Realm’s data models are similar across iOS and Android. - Explain tracking, undo/redo, lazy loading and validation in CoreData ?
Tracking (Change Tracking)
- Core Data automatically keeps track of changes you make to managed objects in memory.
- Even before you call
save()
, Core Data knows this property changed. - So that, you can get lists of inserted, updated, and deleted objects.
- Helps with sync, conflict resolution, and undo operations.
Undo/Redo
- Core Data can integrate with
NSUndoManager
to revert changes step-by-step. - It helps to implement “undo” buttons in apps like note editors, drawing tools, or form inputs.
- We can avoid writing custom rollback logic.
Lazy Loading (Faulting)
- Core Data doesn’t load all data into memory immediately — it loads only what you access.
Example: If you fetch 1000 users but only read thename
of the first one, Core Data won’t load the rest until you touch them. - So it reduces memory usage.
- Improves performance for large datasets.
Validation
- You can set rules to validate data before it’s saved.
Example: Age must be > 0 or Email must contain “@” - It ensures data integrity without extra boilerplate checks everywhere.
Bonus: I’ve attached a zip file of an iOS project with sample code demonstrating Sqlite.swift, CoreData and RealmDB (dependencies added via SPM). Download it.
Happy coding!