ORM for Swift (iOS/macOS): comparison and usage examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

ORM for Swift (iOS/macOS): comparison and examples



When developing applications for iOS and macOS, developers often face the need to store data on the device. To simplify working with databases, ORM (Object-Relational Mapping) libraries are used — libraries that allow you to work with data as regular Swift objects, hiding the complexity of SQL queries. In this article, we will look at four popular solutions: Core Data, Realm, GRDB, and SQLite.swift, compare their capabilities, and provide practical examples.



1. Core Data



Core Data is a framework from Apple, built into iOS and macOS. It is not a pure ORM, but rather a framework for managing an object graph that can use SQLite, XML, or binary storage.



Advantages:

  • Deep integration with the Apple ecosystem (SwiftUI, iCloud, Undo Manager).
  • Visual data model editor (Xcode Data Modeler).
  • Support for versioning and migrations.


Disadvantages:

  • High entry barrier (a lot of boilerplate code).
  • Performance issues when working with large datasets.
  • Tight binding to Apple platforms.


Code example (Swift):

import CoreData

// Creating a contextlet container = NSPersistentContainer(name: "Model")container.loadPersistentStores { _, error in if let error = error { print(error) }}

// Saving an objectlet context = container.viewContextlet user = User(context: context)user.name = "Ivan"user.age = 30

try? context.save()

// Querylet request: NSFetchRequest<User> = User.fetchRequest()request.predicate = NSPredicate(format: "age > 18")let users = try? context.fetch(request)


2. Realm



Realm is a cross-platform ORM designed specifically for mobile applications. It is faster than Core Data on read and write operations.



Advantages:

  • High performance (up to 10x faster than Core Data).
  • Simple API without needing to write SQL.
  • Automatic object updates (live objects).
  • Support for reactive extensions (RxRealm, Combine).


Disadvantages:

  • Proprietary database file format.
  • Limited database size (up to 2 GB on iOS).
  • Less flexibility for complex queries.


Code example (Swift):

import RealmSwift

// Model definitionclass User: Object { @Persisted var name: String = "" @Persisted var age: Int = 0}

// Savinglet realm = try! Realm()let user = User()user.name = "Maria"user.age = 25

try! realm.write { realm.add(user)}

// Querylet users = realm.objects(User.self).filter("age > 18")for user in users { print("\\(user.name): \\(user.age)")}


3. GRDB



GRDB is a modern library for working with SQLite in Swift. It combines the capabilities of low-level SQL access with the convenience of ORM.



Advantages:

  • Maximum performance (native SQLite).
  • Support for Combine and Swift Concurrency (async/await).
  • Full control over SQL queries.
  • Support for migrations and versioning.


Disadvantages:

  • Requires knowledge of SQL.
  • Less "magic" — more manual code.


Code example (Swift):

import GRDB

// Model definitionstruct User: Codable, FetchableRecord, PersistableRecord { var id: Int64? var name: String var age: Int}

// Connecting to the databaselet dbQueue = try DatabaseQueue(path: "/path/to/database.sqlite")

// Savingtry dbQueue.write { db in let user = User(nam

Recommendations