Swiftpack.co - realm/realm-swift as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by realm.
realm/realm-swift vv5.0.0-beta.1
Realm is a mobile database: a replacement for Core Data & SQLite
⭐️ 15,745
🕓 24 weeks ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/realm/realm-swift.git", from: "vv5.0.0-beta.1")

Developing with the Realm SDKs? We want to hear from you! Take our short survey


Realm

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.
  • Device Sync: Makes it simple to keep data in sync across users, devices, and your backend in real-time. Get started for free with a template application that includes a cloud backend and Sync.

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.

// 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<Dog>
}
// 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.

// 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.

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.

// Generate a random encryption key
var key = Data(count: 64)
_ = key.withUnsafeMutableBytes { bytes in
    SecRandomCopyBytes(kSecRandomDefault, 64, bytes)
}

// Add the encryption key to the config and open the realm
let config = Realm.Configuration(encryptionKey: key)
let realm = try Realm(configuration: config)

// Use the Realm as normal
let dogs = realm.objects(Dog.self).filter("name contains 'Fido'")

Getting Started

We support installing Realm via Swift Package Manager, CocoaPods, Carthage, or by importing a dynamic XCFramework.

For more information, see the detailed instructions in our docs.

Interested in getting started for free with a template application that includes a cloud backend and Sync? Create a MongoDB Atlas Account.

Documentation

The documentation can be found at docs.mongodb.com/realm/sdk/ios/.
The API reference is located at docs.mongodb.com/realm-sdks/swift/latest/

Getting Help

  • Need help with your code?: Look for previous questions with therealm tag on Stack Overflow or ask a new question. For general discussion that might be considered too broad for Stack Overflow, use the Community Forum.
  • Have a bug to report? Open a GitHub issue. If possible, include the version of Realm, a full log, the Realm file, and a project that shows the issue.
  • Have a feature request? Open a GitHub issue. Tell us what the feature should do and why you want the feature.

Building Realm

In case you don't want to use the precompiled version, you can build Realm yourself from source.

Prerequisites:

  • Building Realm requires Xcode 11.x or newer.
  • Building Realm documentation requires jazzy

Once you have all the necessary prerequisites, building Realm.framework just takes a single command: sh build.sh build. You'll need an internet connection the first time you build Realm to download the core binary.

Run sh build.sh help to see all the actions you can perform (build ios/osx, generate docs, test, etc.).

Contributing

See CONTRIBUTING.md for more details!

Code of Conduct

This project adheres to the MongoDB Code of Conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to [email protected].

License

Realm Objective-C & Realm Swift are published under the Apache 2.0 license.
Realm Core is also published under the Apache 2.0 license and is available here.

Feedback

If you use Realm and are happy with it, please consider sending out a tweet mentioning @realm to share your thoughts!

And if you don't like it, please let us know what you would like improved, so we can fix it!

GitHub

link
Stars: 15745
Last commit: 14 hours ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Dependencies

Release Notes

v10.35.0
Yesterday

This version bumps the Realm file format version to 23. Realm files written by this version cannot be read by older versions of Realm.

Enhancements

  • The Realm file is now automatically shrunk if the file size is larger than needed to store all of the data. (Core PR #5755)
  • Pinning old versions (either with frozen Realms or with Realms on background threads that simply don't get refreshed) now only prevents overwriting the data needed by that version, rather than the data needed by that version and all later versions. In addition, frozen Realms no longer pin the transaction logs used to drive change notifications. This mostly eliminates the file size growth caused by pinning versions. (Core PR #5440)
  • Rework how Dictionaries/Maps are stored in the Realm file. The new design uses less space and is typically significantly faster. This changes the iteration order of Maps, so any code relying on that may be broken. We continue to make no guarantees about iteration order on Maps (Core #5764).
  • Improve performance of freezing Realms (Core PR #6211).

Fixed

  • Fix a crash when using client reset with recovery and flexible sync with a single subscription (Core #6070, since v10.28.2)
  • Encrypted Realm files could not be opened on devices with a larger page size than the one which originally wrote the file. (#8030, since v10.32.1)
  • Creating multiple flexible sync subscriptions at once could hit an assertion failure if the server reported an error for any of them other than the last one (Core #6038, since v10.21.1).
  • Set<AnyRealmValue> and List<AnyRealmValue> considered a string and binary data containing that string encoded as UTF-8 to be equivalent. This could result in a List entry not changing type on assignment and for the client be inconsistent with the server if a string and some binary data with equivalent content was inserted from Atlas. (Core #4860 and Core #6201, since v10.8.0)
  • Querying for NaN on Decimal128 properties did not match any objects (Core #6182, since v10.8.0).
  • When client reset with recovery is used and the recovery did not need to make any changes to the local Realm, the sync client could incorrectly think the recovery failed and report the error "A fatal error occured during client reset: 'A previous 'Recovery' mode reset from did not succeed, giving up on 'Recovery' mode to prevent a cycle'". (Core #6195, since v10.32.0)
  • Writing to newly in-view objects while a flexible sync bootstrap was in progress would not synchronize those changes to the server (Core #5804, since v10.21.1).
  • If a client reset with recovery or discard local was interrupted while the "fresh" realm was being downloaded, the sync client could crash with a MultpleSyncAgents exception (Core #6217, since v10.25.0).
  • Sharing Realm files between a Catalyst app and Realm Studio did not properly synchronize access to the Realm file (Core #6258, since v10.0.0).

Compatibility

  • Realm Studio: 13.0.2 or later.
  • APIs are backwards compatible with all previous releases in the 10.x.y series.
  • Carthage release for Swift is built with Xcode 14.2.
  • CocoaPods: 1.10 or later.
  • Xcode: 13.3-14.2.

Internal

  • Upgraded realm-core from 12.13.0 to 13.4.0

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics