realm /realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite

★ 16,599⑂ 2,230👁 355⑃ 3,199● 5,500▤ 305
Objective-CApache-2.0Created 2012-04-16realm.io
下载:v20.0.6v20.0.6 ↓20.0.62026-09-27
+0
日增长
16,599
stars
2,230
forks
355
watchers
3,199
prs
5,500
issues
305
releases

README

About Realm Database

Realm is a mobile database that runs directly inside phones, tablets or wearables.
This repository holds the source code for the iOS, macOS, tvOS & watchOS versions of Realm Swift & Realm Objective-C.

 Why Use Realm

 Intuitive to Developers: Realm’s object-oriented data model is simple to learn, doesn’t need an ORM, and lets you write less code.
 Built for Mobile: Realm is fully-featured, lightweight, and efficiently uses memory, disk space, and battery life.
 Designed for Offline Use: Realm’s local database persists data on-disk, so apps work as well offline as they do online.

 Object-Oriented: Streamline Your Code

Realm was built for mobile developers, with simplicity in mind. The idiomatic, object-oriented data model can save you thousands of lines of code.

swift
// Define your models like regular Swift classes
class Dog: Object {
    @Persisted var name: String
    @Persisted var age: Int
}
class Person: Object {
    @Persisted(primaryKey: true) var _id: String
    @Persisted var name: String
    @Persisted var age: Int
    // Create relationships by pointing an Object field to another Class
    @Persisted var dogs: List
}
// Use them like regular Swift objects
let dog = Dog()
dog.name = "Rex"
dog.age = 1
print("name of dog: \(dog.name)")

// Get the default Realm
let realm = try! Realm()
// Persist your data easily with a write transaction
try! realm.write {
    realm.add(dog)
}

 Live Objects: Build Reactive Apps
Realm’s live objects mean data updated anywhere is automatically updated everywhere.
swift
// Open the default realm.
let realm = try! Realm()

var token: NotificationToken?

let dog = Dog()
dog.name = "Max"

// Create a dog in the realm.
try! realm.write {
    realm.add(dog)
}

//  Set up the listener & observe object notifications.
token = dog.observe { change in
    switch change {
    case .change(let properties):
        for property in properties {
            print("Property '\(property.name)' changed to '\(property.newValue!)'");
        }
    case .error(let error):
        print("An error occurred: (error)")
    case .deleted:
        print("The object was deleted.")
    }
}

// Update the dog's name to see the effect.
try! realm.write {
    dog.name = "Wolfie"
}

 SwiftUI
Realm integrates directly with SwiftUI, updating your views so you don't have to.
swift
struct ContactsView: View {
    @ObservedResults(Person.self) var persons

    var body: some View {
        List {
            ForEach(persons) { person in
                Text(person.name)
            }
            .onMove(perform: $persons.move)
            .onDelete(perform: $persons.remove)
        }.navigationBarItems(trailing:
            Button("Add") {
                $persons.append(Person())
            }
        )
    }
}


 Fully Encrypted
Data can be encrypted in-flight and at-rest, keeping even the most sensitive data secure.
swift
// Generate a random encryption key
var key = Data(count: 64)
_ = key.withUnsafeMutableBytes { 

Top issues

Discussions

Latest release

v20.0.6 v20.0.6 2026-09-27
Fix a few warnings when building with Xcode 27 and updating packaging for Xcode 27.

 Compatibility

 Carthage release for Swift is built with Xcode 27.
 CocoaPods: 1.10 or later.
 Xcode: 26.1-27.0

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON