swift - Swift中基于条件的领域更新
问题描述
我有一个对象,如果存储的时间戳小于更新数据中的时间戳,我只想更新它。在 mysql 中,我会设置一个条件,例如UPDATE items SET email="admin@google.com" WHERE timestamp < 1725239737
在领域中执行此操作的最佳方法是什么?
解决方案
你可以做这样的事情。获取所有对象,然后更新电子邮件值:
// 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"
}
}
推荐阅读
- git - Visual Studio 2017 无法推送到 git
- sql - Oracle 12c - 有条件的 where
- highcharts - Highcharts - 隐藏轴影响重绘性能
- user-interface - 如何在应用程序脚本中使用菜单项传递参数
- flowtype - 流程:在使用 Yarn 工作区的 monorepo 中解析模块
- javascript - TypeError:未定义不是对象(评估'BMDPedometer.isStepCountingAvailable')
- java - 无法在 Java 中找到字符串中的模式数量
- amazon-web-services - AWS Appsync - 找不到用户池客户端 ID
- excel - 将“^=^”替换为“=”,其中结果仍为文本
- python - Python gsl_vector_set