ORM for Swift (iOS/macOS): Comparison and Examples
When developing applications for iOS and macOS, working with local data is one of the key tasks. Object-Relational Mapping (ORM) allows developers to work with databases using an object-oriented approach instead of writing raw SQL queries. In this article, we will look at four main ORM solutions for Swift: Core Data, Realm, GRDB, and SwiftData. You will learn their strengths and weaknesses, and see code examples for each technology.
1. Core Data
Core Data is a framework from Apple, built into iOS, macOS, tvOS, and watchOS. It is not a pure ORM (rather, a persistence framework), but it performs all ORM functions: managing the object model, saving, rolling back changes, and binding to the UI via NSFetchedResultsController.
Advantages:
- Deep integration with the Apple ecosystem (iCloud, SwiftUI, Combine)
- Built-in support for versioning and data migration
- Powerful predicates and sorting
Disadvantages:
- Complex setup and a lot of boilerplate code
- Problems with multithreading (strict context required)
- Low performance with large data volumes
Usage Example:
import CoreData
// 1. Create a model (in Xcode Data Model Editor)// 2. Initialize the containerlet container = NSPersistentContainer(name: "MyModel")container.loadPersistentStores { _, error in if let error = error { fatalError("Failed to load: \\(error)") }}
// 3. Save an objectlet context = container.viewContextlet user = User(context: context)user.name = "Ivan"user.age = 30try? context.save()
// 4. Querylet request: NSFetchRequest<User> = User.fetchRequest()request.predicate = NSPredicate(format: "age > %@", 18)let adults = try? context.fetch(request)2. Realm
Realm is a popular third-party ORM created for mobile platforms. It works faster than Core Data in most scenarios and is easier to use. Realm uses its own storage engine, not SQLite.
Advantages:
- High read/write speed
- Simple API (no NSManagedObject)
- Built-in support for reactive updates (via Realm Notifications)
Disadvantages:
- Closed source (not fully open-source)
- Migration issues when changing the model
- Limited support for complex queries (JOIN, subqueries)
Usage Example:
import RealmSwift
// 1. Define the modelclass User: Object { @Persisted var name: String = "" @Persisted var age: Int = 0}
// 2. Create and savelet realm = try! Realm()let user = User()user.name = "Maria"user.age = 25try! realm.write { realm.add(user)}
// 3. Querylet adults = realm.objects(User.self).filter("age > 18")3. GRDB
GRDB is a lightweight ORM for Swift built on top of SQLite. It gives you full control over SQL queries while providing a convenient object-oriented interface. GRDB is especially popular among developers who want to combine the power of SQL with the convenience of Swift.
Advantages:
- Full access to SQL (can write complex queries)
- High performance (close to raw SQLite)
- Support for Swift Concurrency (async/await)
Disadvantages:
- Requires knowledge of SQL
- Less documentation than Core Data
- No built-in iCloud support
Usage Example:
import GRDB
// 1. Define the model (Codable + FetchableRecord)struct User: Codable, FetchableRecord, PersistableRecord { var id: Int64? var name: String