0

Could you please guid me how run a background activity/action every 5 seconds.

This activity is running on all screens.

Thank you

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
Zuhair Ali
  • 597
  • 1
  • 5
  • 18
  • Can you provide a little more info on the activity you want to run? – Cocoadelica Dec 18 '14 at 12:14
  • I am working on an app and i would like to check internet connectivity every 5 seconds. I will show a message on the current screen if it is offline. – Zuhair Ali Dec 18 '14 at 12:17
  • 1
    Ah! OK. There's a better way. Apple provides some code for a Reachability class. This fires a notification when the network connectivity state changes. You can observe the notification to get an update when the state changes. It's better than continually running a check on a 5 second cycle. Do a search for Reachability in the sample code section of developer.apple.com – Cocoadelica Dec 18 '14 at 12:24
  • Note that the Readability class is Obj-C but it'll be distinct from your Swift code, you just register to be notified. – Cocoadelica Dec 18 '14 at 12:24
  • Thank you, but I am already using Reachability, but I would like to know how can I run an activity in background that checks Reachability status every 5 seconds – Zuhair Ali Dec 18 '14 at 12:28
  • You can use `NSTimer` (the typical approach) or GCD dispatch timer (if you really need this to run on background thread), both of which can be found with a quick search online. But to use either of these to check connectivity represents a fundamental misunderstanding of the capabilities of Reachability. With Reachability, register for notification rather than checking every five seconds. – Rob Dec 18 '14 at 12:45
  • Ok, actually I am not sure what is the best approach to achieve this goal, so let me explain it is another way. I need to show the internet connectivity status to the user below the navigation title as shown on the attached image. This status will switch from online to offline (and vis versa) automatically when internet connectivity changed. http://i.imgur.com/dK40QRr.png So, is there an optimised way to do that. Thank you – Zuhair Ali Dec 18 '14 at 13:06
  • Look at the code in `APLViewController.m` and that illustrates how to add observer for [Reachability](https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html) notifications. Use that mechanism rather than timers. – Rob Dec 18 '14 at 13:16

0 Answers0