• Github每日精选(第35期):移动设备数据库Realm


    Realm

    Realm 是一个直接在手机、平板电脑或可穿戴设备中运行的移动数据库。这个存储库包含 iOSmacOStvOSwatchOS 版本的 Realm Swift Realm Objective-C 的源代码。

    github 下的地址在这里
    在这里插入图片描述

    使用领域
    • 对开发人员直观: Realm 的面向对象数据模型易于学习,不需要 ORM,并且可以让您编写更少的代码。
    • 专为离线使用而设计: Realm 的本地数据库将数据保存在磁盘上,因此应用程序离线工作与在线工作一样好。
    • 专为移动设备打造: Realm 功能齐全、重量轻,可有效利用内存、磁盘空间和电池寿命。
    面向对象:简化您的代码

    Realm 是为移动开发人员构建的,并考虑到了简单性。惯用的、面向对象的数据模型可以为您节省数千行代码。

    // 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)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    Live Objects

    Realm 的活动对象意味着在任何地方更新的数据都会在任何地方自动更新。

    // 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"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    SwiftUI

    Realm 直接与 SwiftUI 集成,无需更新视图即可。

    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())
                }
            )
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    完全加密

    数据可以在动态和静态加密,即使是最敏感的数据也能保持安全。

    // 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'")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    数据同步

    设备同步服务让用户、设备和后端之间的数据实时同步变得简单。

  • 相关阅读:
    JVM 三种常量池:Class、字符串、基本类型
    软考-信息系统项目管理师- 第 1 章 信息化和信息系统基础知识
    简单说说关于shell中zsh和bash的选择
    慢慢欣赏linux 进程unattended-upgr CPU占用率过高定位
    【一起学数据结构与算法】顺序表的实现
    Bubble Fish Park
    Docker consul 容器服务自动发现和更新
    基于深度学习的电动自行车头盔佩戴检测系统
    为什么JWT要结合Redis使用
    拒绝服务攻击(Dos)与Tomcat的解决方法
  • 原文地址:https://blog.csdn.net/weixin_40425640/article/details/126220950