Observing a value of a static var in a class?
Asked Answered
T

4

8

I have a class with a static var where the current online connection status is stored. I want to observe the value of ConnectionManager.online through other classes. I wanted to do this with KVO, but declaring a static variable as dynamic causes an error:

class ConnectionManager: NSObject {
    dynamic static var online = false
    // adding 'dynamic' declaration causes error:
    // "A declaration cannot be both 'final' and 'dynamic'
}

What is a most elegant way of doing this?

Update. This my code for the KVO part:

override func viewDidLoad() {
    super.viewDidLoad()

    ConnectionManager.addObserver(
        self,
        forKeyPath: "online",
        options: NSKeyValueObservingOptions(),
        context: nil
    )
}

override func observeValueForKeyPath(keyPath: String?, 
                                     ofObject object: AnyObject?, 
                                     change: [String : AnyObject]?, 
                                     context: UnsafeMutablePointer<Void>) {
    if keyPath == "online" {
        print("online status changed to: \(ConnectionManager.online)")
        // doesn't get printed on value changes
    }
}
Theotokos answered 6/7, 2016 at 22:34 Comment(0)
T
8

As for now, Swift cannot have observable class properties. (In fact, static properties are just global variables with its namespace confined in a class.)

If you want to use KVO, create a shared instance (singleton class) which has online property and add observer to the instance.

Turnpike answered 6/7, 2016 at 23:7 Comment(1)
Thanks, will try the singleton pattern.Theotokos
T
5

I solved it with the singleton pattern suggested by @OOper.

class ConnectionManager: NSObject {
    static let sharedInstance = ConnectionManager()
    private override init() {} // This prevents others from using the default '()' initializer for this class.
    @objc dynamic var online = false
}

Then:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.tableFooterView = UIView()

    ConnectionManager.sharedInstance.addObserver(self,
                         forKeyPath: "online",
                         options: [.new, .initial],
                         context: nil)
}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if object is ConnectionManager && keyPath == "online" {
        // ...
    }
}
Theotokos answered 7/7, 2016 at 9:35 Comment(1)
added @objc per the errors here: #46420393Emiliaemiliaromagna
D
2

Try replacing dynamic static var online = false to @nonobjc static var online = false

What's happening is that because it inherits from NSObject, Swift is trying to generate getters and setters for it. Because you are creating it in swift, using the @nonobjc attribute solves the problem.

EDIT:

I don't believe you can observe static variables through KVO because of how it works

Here is a link and snippet from Apple's Guide on KVO

Unlike notifications that use NSNotificationCenter, there is no central object that provides change notification for all observers. Instead, notifications are sent directly to the observing objects when changes are made.

Perhaps, instead of using KVO, you could declare online like:

static var online = false {
    didSet{
        //code to post notification through regular notification center
    }
}

If you're set on using it, this question might point you towards the right direction — it'll involve diving deeper into how KVO works: Is it possible to set up KVO notifications for static variables in objective C?

Diaconicum answered 6/7, 2016 at 22:44 Comment(3)
@Theotokos Updated answerDiaconicum
I also thought of NSNotification but I am kind of surprised that there is no other way to observe static vars. But I think it is the way to go then. Also: Why not use didSet instead of set?Theotokos
@Theotokos Whoops, my bad. I meant didSet. You're right, though, that is kind of strange as it's a pretty useful feature. Maybe it'll come in the future!Diaconicum
L
0

I would suggest property wrapper, I tried the example below and worked perfectly for me:

    @propertyWrapper
    struct StaticObserver<T> {
        private var value:T
        init(value:T) {
            self.value = value
        }
        var wrappedValue: T {
        get {
            // Do your thing
            return self.value
        }
        set {
            // Do your thing before set 
            self.value = newValue
            // Do your thing after set
        }
    }

    @StaticObserver(value: false)
    dynamic static var online:Bool
Lifesaving answered 1/5, 2020 at 5:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.