斯威夫特3:单身人士的财产观察员 [英] Swift 3: Property observer for singleton

查看:109
本文介绍了斯威夫特3:单身人士的财产观察员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法观察单身类的属性ony的变化

I wonder if there's any way to observe singleton class for changes in ony of its property

在我的情况下,使用Realm我有类似的东西

In my case, with Realm I have something like this

class User: Object {
   dynamic var name:String = ""
   dynamic var email:String = ""
   dynamic var id:String = ""
   dynamic var picURL:String = ""
   dynamic var pic:Data = Data()

    static let currentUser = User()
    {
        didSet
        {
            try! realm.write {
                realm.add(currentUser, update: true)
            }
    }
  }
}

我想要实现的是我想在我的应用程序中只有一个用户对象,并且任何时候任何属性都被修改,我想保存它到领域。

What I want to achieve is I want to have only one user object in my app, and anytime any of its properties gets modified, I would like to save it to Realm.

但是,上面的示例显示错误

However, the example above shows error that


'let '声明不能观察属性

'let' declarations cannot be observing properties

你会如何接近它?你认为我的想法是让Realm对象单例并随时更新它的一些属性改变是个好主意吗?

How would you approach it? Do you think my idea with having Realm object singleton and updating it anytime some property changes is a good idea?

我想知道单个对象是否真的有意义Realm持久化的类。

I wonder if it actually makes any sense to have a singleton object of class that is persisted by Realm.

推荐答案

单个Realm 对象无法拥有全局观察者c $ c>目前,有在Realm GitHub中跟踪它的问题而我相信目前正在优先考虑。

It's not possible to have a blanket observer for a single Realm Object at the moment, however there's an issue tracking it in the Realm GitHub and I believe it's being prioritized at the moment.

Alcivanio是正确的,你需要手动触发更新事件到确保更改持久保存到Realm。话虽这么说,因为Realm控制持久化对象中的访问器,所以一旦将对象保存到Realm,就不能保证调用 didSet 。为简单起见,我建议使用闭包来更新用户对象。

Alcivanio is correct in that you'll need to manually trigger an update event to ensure the changes are persisted to Realm. That being said, because Realm controls the accessors in persisted objects, didSet isn't guaranteed to be called once an object has been saved to Realm. For simplicity reasons, I'd recommend using a closure to update the user object.

更新后的答案: 如果您的单例是Realm Object ,则无法在写入事务之外修改其属性。就像我在下面写的那样,最简单的方法是使用一个更新闭包来管理Realm写入事务,然后只需更新里面的Singleton对象的属性它。

Updated Answer: If your singleton is a Realm Object, you can't modify its properties outside of a write transaction. Like I wrote below, the easiest thing to do would be to be to have an update closure that managed the Realm write transaction, and you simply update the properties of your Singleton object inside it.

正如您所看到的,您不能使用 didSet 来使用<$定义的属性c $ c> let ,所以最好回退到Objective-C约定并使用内部实例变量。

As you've also seen, you can't use didSet with a property defined with let, so it might be best to drop back to Objective-C conventions and use an internal instance variable.

所以全部起来,也许考虑这样做:

So all up, maybe consider doing this:

fileprivate var _current: User? = nil

class User: Object {
    dynamic var name:String = ""
    dynamic var email:String = ""
    dynamic var id:String = ""
    dynamic var picURL:String = ""
    dynamic var pic:Data = Data()

    static var current: User
    {
        if _current == nil {
            let realm = try! Realm()

            // Query for an existing user
            _current = realm.objects(User.self).first

            // Create a new user if it didn't exist in Realm
            if _current == nil {
                 _current = User()
                 try! realm.write { realm.add(_current) }
            }
        }
        return _current
    }

    public func update(_ block: (() -> Void)) {
        let realm = try! Realm()
        try! realm.write(block)
    }
}

let user = User.current
user.update {
    user.name = "DCDC"
}

这篇关于斯威夫特3:单身人士的财产观察员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆