0

I have built this struct:

struct Constants {
    static let BASE_URL = "http://\(CameraManager.ip)/Nexus.cgi?"
    static let WHOIAM_URL = "\(Constants.Url.BASE_URL)action=SERVERWhoAmI"
    static var SERVERPING_URL = "\(Constants.Url.BASE_URL)session=\(CameraManager.session)&action=SERVERPing"
}

And the usage is as follows:

    func refreshSession(success: @escaping () -> Void, failure: @escaping (String) -> Void) {
        self.manager.request(Constants.Url.WHOIAM_URL, method:.get).authenticate(usingCredential: self.utiles.getDigestCredential()).responseJSON { (responseObject) -> Void in

            if responseObject.result.isSuccess {
                let value = responseObject.result.value
                let json = JSON(value!)
                print(CameraManager.session)
                if self.parser.isError(json: json).0 {
                    failure(self.parser.isError(json: json).1)
                } else {
                    self.failedPingCounter = 0
                    CameraManager.session = self.parser.parseWhoAmICall(json: json)
                    success()
                }
            }
      }

   func keepAlive(session: String, success: @escaping () -> Void, failure: @escaping (String) -> Void) {

    self.manager.request(Constants.Url.SERVERPING_URL, method:.get).authenticate(usingCredential: self.utiles.getDigestCredential()).responseJSON { (responseObject) -> Void in


}

As you can see I am updating the CameraManager.session value. After updating it I am using it in keepAlive, yet when I check the request, I can see it uses the old one...

What is the best way to achieve what I want?

2
  • If your values aren't actually constants, don't treat them as such. You will run into these kinds of issues and your code will be harder to read and understand. In this case you'll probably just want to use computed properties instead. Commented Apr 21, 2019 at 9:43
  • @Keiwan can you give an example? Commented Apr 21, 2019 at 9:55

1 Answer 1

1

Static vars are implicitly lazy which is why it's not changing even after you change your CameraManager.session value. Since these values are changing I wouldn't make them static and just make them normal computed variables.

Sign up to request clarification or add additional context in comments.

5 Comments

So how would you use it if not as static? i need a lot of parts from the app to have access to it? Can you elaborate?
remove the word static in your Constants struct. You'd have to instantiate a constant struct to access them then though.
Or you could have static var SERVERPING_URL: String { return "\(Constants.Url.BASE_URL)session=\(CameraManager.session)&action=SERVERPing" }. That way it will execute that code every time you request it instead of being lazy.
So i have used static var SERVERPING_URL: String { return "(Constants.Url.BASE_URL)session=(CameraManager.session)&action=SERVERPing" }. Seems to work fine. So as i understand this because that i calculate the value every time?
yeah that's right. To me that's fine because it's not that computationally expensive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.