1

I have to call API every second to get updated values. But I have some question regarding best practices to handle this

  1. Do I need to stop the timer if the internet connection is OFF?
  2. If #1 yes then is it necessary to start API call automatically or should I ask user to try again?
  3. Assuming API call should resume automatically then how? Should I check reachability every second?
  4. Since I have to call API every second, so how to handle request timeout and prevent multiple calls for the same request? Do you think I should be worried about this?

Note:

I really want to know best way (standard practices) to do this. I don't want to use third party library.

Better design and optimized way are welcome

Community
  • 1
  • 1
Nick
  • 1,127
  • 2
  • 15
  • 40
  • 2
    I think you should be worried about an architecture that requires you to poll an endpoint every second; You should be looking at a better approach such as web sockets or push notifications. – Paulw11 Jul 12 '19 at 03:12
  • @Paulw11 I thought about sockets and notification but it's not needed. It's simple GET request need to call every second. – Nick Jul 12 '19 at 03:27
  • But that isn't very efficient for battery life or network traffic – Paulw11 Jul 12 '19 at 03:27
  • I agree with you, the deal way to use socket/notification but this how our current architecture is. And this was the actual reason I posted this question on StackOverflow. Is there a better way to handle this from App? – Nick Jul 12 '19 at 03:32
  • I probably wouldn’t get too fancy. Just try and connect and it fails it fails. Apple advises against preflight connectivity checks in the 2018 networking wwdc session since the network status can change just after your check. You could set a flag that indicates if your previous call is complete in case your timer fires before it has; you can then defer that call – Paulw11 Jul 12 '19 at 03:58
  • @Paulw11 With that said I have to run timer forever? – Nick Jul 12 '19 at 04:20
  • Is your app destined for the App Store? You can’t run a timer in the background or run forever without a hack like abusing background audio playback. And this will serious impact battery life. – Paulw11 Jul 12 '19 at 04:22
  • @Paulw11 Yes, any other alternative? – Nick Jul 12 '19 at 04:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196353/discussion-between-paulw11-and-nick). – Paulw11 Jul 12 '19 at 05:15
  • @Nick I am in the same boat. Can you please share if you figured out a better way to deal with it? – Debi Oct 16 '20 at 18:59
  • Hey @Debi, I am not sure it was an ideal solution but yes I used a reachability notifier to get the internet connection status. On an active internet connection every successful call the next call is scheduled automatically and if the call fails more than 10 times consecutively the scheduler will stop and won't start unless the user press "try again" or reachability status changes (This will get internal notification). – Nick Oct 17 '20 at 16:23

0 Answers0