【问题标题】:Swift Realm, write into database not working in appdelegate when app is in backgroundSwift Realm,当应用程序处于后台时,写入数据库在appdelegate中不起作用
【发布时间】: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)

}

【问题讨论】:

标签: ios swift realm


【解决方案1】:

领域插入代码看起来不错,请检查您是否在signing & Capabilities 中启用了Background modes

【讨论】:

  • 是的,我启用了Background fetchRemote notificationsBackground processing,但是没有用,我设置了断点,try! realm.write() { 不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-06
  • 1970-01-01
  • 2013-05-20
  • 2021-11-28
  • 2017-08-11
  • 1970-01-01
相关资源
最近更新 更多