0

I'm having some trouble with finding a way to update something. I have this function:

func pingHost(_ fullURL: String) -> Bool {
        let url = URL(string: fullURL)
        let task = URLSession.shared.dataTask(with: url!) { _, response, _ in
            if let httpResponse = response as? HTTPURLResponse {
                if httpResponse.statusCode == 200 {
                    self.status = true
                } else if httpResponse.statusCode != 200 {
                    self.status = false
                }
            }
        }
        task.resume()
        return self.status
    }

I want this function to loop every second. I already tried a while loop in the viewDidLoad function. But if I want to add UI it can't load, because of the while (The while loop never ends, so the viewDidLoad is never gonna be executed properly).

The viewDidLoad:

override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.purple

        statusLabel.translatesAutoresizingMaskIntoConstraints = false

        self.view.addSubview(statusLabel)
        statusLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        statusLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 125).isActive = true
        statusLabel.text = "Status Label"
        statusLabel.textAlignment = .left
        statusLabel.textColor = UIColor.white
        // Do any additional setup after loading the view.
        while true {
            if (self.pingHost("http://example.com") as Bool) != false {
                print("URL is online")
            } else if (self.pingHost("http://example.com") as Bool) != true {
                print("URL doesn't give a response back")
            }
        }
    }

I hope someone is able to help me with this problem. Thanks in advance!

Sulthan
  • 128,090
  • 22
  • 218
  • 270
mHvNG
  • 671
  • 5
  • 22
  • Why you need to call that function like that ? Also the while loop will be called multiple times within a second and why are you spamming your own server by sending this much amount of requests ? – Midhun MP Oct 21 '19 at 11:26
  • @MidhunMP Your question: Why you need to call that function like that? I don't, thats my question: Whats the correct way... The while loop indeed requests more than one second, but as i said i knew it wasn't the correct way. – mHvNG Oct 21 '19 at 11:33
  • If you want to simply check whether network or server is available use Reachability library. I asked 'Why you need' means what is your intention of calling that method in each 1 second, what you are going to achieve by that? Is that for checking whether server is up and running ? – Midhun MP Oct 21 '19 at 12:09

1 Answers1

3

You can set up a timer in viewDidLoad: first declare it as a variable:

var timer: Timer?

Then in viewDidLoad:

timer = Timer.scheduledTimer(withTimeInterval: 1.0,
                             repeats: true,
                             block: { [weak self] _ in
                                  self?.pingHost("http://example.com")
})
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
Lu_
  • 2,577
  • 16
  • 24
  • 1
    @mHvNG make sure you `invalidate` your timer when needed. For more see [here](https://stackoverflow.com/questions/27416896/what-is-difference-between-self-timer-nil-vs-self-timer-invalidate-in-ios/52544614#52544614) – mfaani Oct 21 '19 at 11:56