1

How do you continuously send HTTP GET requests on a time interval (every x/seconds)?

Currently I have a getMessages() function in my viewDidLoad() so that it will send a GET request on view load and populate a table view with the results from the server.

c0de
  • 819
  • 2
  • 9
  • 21

2 Answers2

1

Just use the NSTimer method scheduledTimerWithTimeInterval. You can say that it should repeat the timer and which method should get invoked if the time is over:

NSTimer.scheduledTimerWithTimeInterval(timeInSeconds, target: self, selector: "getMessages", userInfo: nil, repeats: true)
Christian
  • 22,585
  • 9
  • 80
  • 106
  • Since network requests can take unbounded time it would be better to fire off a new one each time a request succeeds or fails. How could I implement this? – c0de Feb 19 '15 at 20:56
  • 1
    Well you can do it inside the getMessages method. – Christian Feb 19 '15 at 21:18
0

with swift3, use Timer, for more refer to this send http request at intervals.

Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(getMessage), userInfo: nil, repeats: true)
Community
  • 1
  • 1
LF00
  • 27,015
  • 29
  • 156
  • 295