首页 > 解决方案 > Swift中基于条件的领域更新

问题描述

我有一个对象,如果存储的时间戳小于更新数据中的时间戳,我只想更新它。在 mysql 中,我会设置一个条件,例如UPDATE items SET email="admin@google.com" WHERE timestamp < 1725239737在领域中执行此操作的最佳方法是什么?

标签: swiftrealm

解决方案


你可以做这样的事情。获取所有对象,然后更新电子邮件值:

// Query all objects with Item Realm Object Class
let datetime = Date(timeIntervalSince1970: 1725239737)
let items = realm.objects(Item.self).filter("timestamp < %@", datetime)

// Write to Realm
let realm = try! Realm()
try! realm.write {
    for item in items {       
        item.email = "admin@google.com"
    }
}

https://realm.io/docs/swift/latest/#filtering


推荐阅读