【发布时间】:2020-08-20 06:42:49
【问题描述】:
在我的应用程序中,当收到新的推送通知时,我想将一些数据插入到我的 Realm 数据库中,但问题是如果应用程序在后台数据不写入数据库但如果用户在通知栏中触摸通知,那么一切正常!
以下是我将数据插入 Realm DB 的代码:
let realm = try! Realm()
try! realm.write() {
let notif = NotificationDBModel()
notif.id = id
if let title = data["title"] {
notif.title = title as! String
}
if let content = data["content"] {
notif.content = content as! String
}
if let date = data["date"] {
notif.date = date as! String
}
realm.add(notif)
}
【问题讨论】:
-
您问题中的代码对我来说很好用。这表明您在该代码之外的某个地方遇到了问题。请花点时间查看How do I ask a good question? 和How to create a Minimal, Complete, and Verifiable example。哦,您的
NotificationDBModel可能是个问题,但我们不知道它是什么样子。你能用那个对象更新问题吗?