realm-swift README

The project's README from github.com/realm/realm-swift. As one markdown file: README.md.

realm

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

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 { (pointer: UnsafeMutableRawBufferPointer) in
    guard let baseAddress = pointer.baseAddress else {
        fatalError("Failed to obtain base address")
    }
    SecRandomCopyBytes(kSecRandomDefault, 64, baseAddress)
}

// 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 our Quick Start.

Documentation

The documentation can be found in the docs/ directory.

The API reference can be generated from source using jazzy by running sh build.sh docs from the root of this repository.

Getting Help

Building Realm

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

Prerequisites:

Once you have all the necessary prerequisites, building Realm 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. This will produce Realm.xcframework and RealmSwift.xcframework in build/Release/.

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!

realm-swift (realm/realm-swift)

Searching Database…

这是做什么的?Realm 是一个开源的移动数据库,直接运行在手机、平板和可穿戴设备上——它是 Core Data 和 SQLite 的嵌入式替代品,而不是客户端-服务器数据库。
它和直接用 Core Data 或 SQLite 有什么不同?Realm 提供面向对象的数据模型,不需要 ORM,能帮你省掉不少代码,而且它专门针对移动设备的内存、磁盘空间和电池续航等限制做了优化。
我怎么把它加到我的 Xcode 项目里?可以通过 Swift Package Manager、CocoaPods 或 Carthage 安装,也可以直接导入一个动态 XCFramework。
它在安卓上也能用吗,还是只支持苹果设备?只支持苹果平台:这个仓库里是 Realm Swift 和 Realm Objective-C 的 iOS、macOS、tvOS 和 watchOS 版本——没有安卓或 Windows 的代码。
手机没网的时候,我的应用会怎么样?不会有任何问题——Realm 的本地数据库会把数据保存在磁盘上,所以应用离线和在线时都能正常工作。
存在设备上的数据安全吗?可以很安全:Realm 支持对传输中和静态存储的数据都进行加密,即使是敏感数据也能得到保护。
我能把它用在我自己的应用里发布吗,包括付费应用?可以——Realm Objective-C、Realm Swift 和 Realm Core 都采用 Apache 2.0 许可证发布。
它和 SwiftUI 兼容吗?兼容,它可以直接和 SwiftUI 集成,自动更新你的视图,不用你手动处理。它的实时对象还意味着任何地方修改的数据都会自动同步到应用中的所有位置。
我能在后台线程里放心地用它吗?可以——线程安全是 Realm 宣传的特性之一(该项目带有 'threadsafe' 标签),所以从设计上就支持多个线程并发访问。
Creator
realm
Size
66.3 MB
Stars
16.6K
Age
14y
Commits · 90 Days