项目作者: realm

项目描述 :
Realm是一个移动数据库:Core Data和SQLite的替代品
高级语言: Objective-C
项目地址: git://github.com/realm/realm-cocoa.git
创建时间: 2012-04-16T20:06:56Z
项目社区:https://github.com/realm/realm-cocoa

开源协议:Other

下载


[!WARNING]
We announced the deprecation of Atlas Device Sync + Realm SDKs in September 2024. For more information please see:

For a version of realm-swift without sync features, install version 20 or see the community branch.



realm by MongoDB

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.
  • MongoDB Atlas 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 and create the cloud backend.

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.

  1. // Define your models like regular Swift classes
  2. class Dog: Object {
  3. @Persisted var name: String
  4. @Persisted var age: Int
  5. }
  6. class Person: Object {
  7. @Persisted(primaryKey: true) var _id: String
  8. @Persisted var name: String
  9. @Persisted var age: Int
  10. // Create relationships by pointing an Object field to another Class
  11. @Persisted var dogs: List<Dog>
  12. }
  13. // Use them like regular Swift objects
  14. let dog = Dog()
  15. dog.name = "Rex"
  16. dog.age = 1
  17. print("name of dog: \(dog.name)")
  18. // Get the default Realm
  19. let realm = try! Realm()
  20. // Persist your data easily with a write transaction
  21. try! realm.write {
  22. realm.add(dog)
  23. }

Live Objects: Build Reactive Apps

Realm’s live objects mean data updated anywhere is automatically updated everywhere.

  1. // Open the default realm.
  2. let realm = try! Realm()
  3. var token: NotificationToken?
  4. let dog = Dog()
  5. dog.name = "Max"
  6. // Create a dog in the realm.
  7. try! realm.write {
  8. realm.add(dog)
  9. }
  10. // Set up the listener & observe object notifications.
  11. token = dog.observe { change in
  12. switch change {
  13. case .change(let properties):
  14. for property in properties {
  15. print("Property '\(property.name)' changed to '\(property.newValue!)'");
  16. }
  17. case .error(let error):
  18. print("An error occurred: (error)")
  19. case .deleted:
  20. print("The object was deleted.")
  21. }
  22. }
  23. // Update the dog's name to see the effect.
  24. try! realm.write {
  25. dog.name = "Wolfie"
  26. }

SwiftUI

Realm integrates directly with SwiftUI, updating your views so you don’t have to.

  1. struct ContactsView: View {
  2. @ObservedResults(Person.self) var persons
  3. var body: some View {
  4. List {
  5. ForEach(persons) { person in
  6. Text(person.name)
  7. }
  8. .onMove(perform: $persons.move)
  9. .onDelete(perform: $persons.remove)
  10. }.navigationBarItems(trailing:
  11. Button("Add") {
  12. $persons.append(Person())
  13. }
  14. )
  15. }
  16. }

Fully Encrypted

Data can be encrypted in-flight and at-rest, keeping even the most sensitive data secure.

  1. // Generate a random encryption key
  2. var key = Data(count: 64)
  3. _ = key.withUnsafeMutableBytes { (pointer: UnsafeMutableRawBufferPointer) in
  4. guard let baseAddress = pointer.baseAddress else {
  5. fatalError("Failed to obtain base address")
  6. }
  7. SecRandomCopyBytes(kSecRandomDefault, 64, baseAddress)
  8. }
  9. // Add the encryption key to the config and open the realm
  10. let config = Realm.Configuration(encryptionKey: key)
  11. let realm = try Realm(configuration: config)
  12. // Use the Realm as normal
  13. 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 mongodb.com/docs/atlas/device-sdks/sdk/swift/.
The API reference is located at mongodb.com/docs/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 14.1 or newer.
  • Building Realm documentation requires jazzy

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 conduct@mongodb.com"">community-conduct@mongodb.com.

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!