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?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
ironRoei
  • 2,049
  • 24
  • 45
  • 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. – Keiwan Apr 21 '19 at 09:43
  • @Keiwan can you give an example? – ironRoei Apr 21 '19 at 09:55

1 Answers1

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.

Tom Pearson
  • 384
  • 1
  • 8
  • 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? – ironRoei Apr 21 '19 at 10:24
  • remove the word `static` in your `Constants` struct. You'd have to instantiate a constant struct to access them then though. – Tom Pearson Apr 21 '19 at 10:25
  • 1
    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. – Tom Pearson Apr 21 '19 at 10:27
  • 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? – ironRoei Apr 21 '19 at 12:08
  • yeah that's right. To me that's fine because it's not that computationally expensive. – Tom Pearson Apr 21 '19 at 15:38