I wonder if there's any way to observe singleton class for changes in ony of its property
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' declarations cannot be observing properties
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?
I wonder if it actually makes any sense to have a singleton object of class that is persisted by Realm.
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 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.
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.
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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With