OGeek|极客世界-中国程序员成长平台

标题: ios - Realm 数据库 - 删除对象时的通知 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:23
标题: ios - Realm 数据库 - 删除对象时的通知

我的问题很简单:有什么方法可以在对象实例即将被删除时得到通知?

我有一个属性是存储在磁盘上的图像的路径。每当删除该类型的 Realm 对象时,我想删除此图像。



Best Answer-推荐答案


如果您不想使用 KVO,可以利用 Realm 的 Object Notifications .

一个简短的例子是:

var token: NotificationToken?
token = yourRealmObject.addNotificationBlock { change in
    switch change {
    case .change(let properties):
        print("Object has changed")
    case .error(let error):
        print("An error occurred: \(error)")
    case .deleted:
        print("The object was deleted.")
    }
}

只要确保保留对 token 对象的强引用,因为通知 block 一发布就会被 Realm 自动取消订阅。

关于ios - Realm 数据库 - 删除对象时的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37319335/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4