0

This might be very basic questions but I have spent enough time to explore the possible solutions. In my project, I am successfully getting status info via service request. I want to add a continuous observer for any state change on the server. I am not sure if the server is setup to push silent notifications. It appears KVC/KVO or Notifications only observers changes in objects. How can I elegantly approach this problem to refresh my view only when the status changes on the server?

Obj-Swift
  • 2,802
  • 3
  • 29
  • 50
  • What is service request? How exactly are you getting the status info, where is the code for getting the status info? – Gruntcakes May 21 '15 at 21:05
  • A GET request is providing me with status info ex: if the status is on/off. So on view load I am getting+displaying the status if it is on or off. but I need to update it in real time when the status changes on server. – Obj-Swift May 21 '15 at 21:09
  • 2
    But a HTTP GET request is a discrete event, not a continuous event. Your question is nothing to do with observing changes in iOS objects. Your question is how to communicate with a server in real time. The only way is to replace the implementation of HTTP with raw sockets and keep a socket connection open at all times (which won't be possible when your app is in the background). Or for the server to send a silent push notification, but silent push notifications are no longer feasible for this with iOS8 as they are only delivered quickly when the phone is charging. – Gruntcakes May 21 '15 at 21:16
  • Thats some solid info. Thanks. That means its not possible unless changes are made on server side correct? This is the first time I am looking into listener implementation and trying to learn as much as I can. – Obj-Swift May 21 '15 at 21:20
  • You'd need to make changes to the iOS app too if you use sockets (if you are a beginner this will be too complex). The other alternative is for you to send a GET every 10 seconds for example. Not real time but real time +10. – Gruntcakes May 21 '15 at 21:26
  • Good to know that. I fear sending GET every 10 seconds won't be an efficient way but should work for now. Thanks for the info. – Obj-Swift May 21 '15 at 21:48

2 Answers2

2

You can poll the server continuously, or in my opinion, a much better solution is to use WebSockets. There's a variety of libraries out there for both iOS and server-side.

chedabob
  • 5,835
  • 2
  • 24
  • 44
2

I approach such a development task like this:

  • Use the performSelectorInBackground:withObject: method to create a concurrent thread.
  • Use an NSURLConnection to perform an HTTP GET operation (synchronously) against your server, allowing the thread to block until a reply arrives.
  • Use an NSEvent object to notify the main thread when a response is available for inspection.
  • Repeat the above, as needed.

For state changes that occur less frequently, I would use the Apple Push Notification Service to send state change information to my app, without the need for continuous polling.

Daniel Randall
  • 351
  • 2
  • 13
  • For step #3, what is the adv. of using `NSEvent` vs keeping your logic in the background and accessing the main thread via `dispatch_async(dispatch_get_main_queue()` ? – DaynaJuliana May 21 '15 at 22:09
  • 1
    @DaynaJuliana - The only benefit I get is having more low-level and granular source code; I unit-tested a 10-second-repeat-polling thread using dispatch queues and was unhappy with the performance, so I rolled my own and the performance was exactly what I wanted; and it was easy to maintain when the specifications changed. – Daniel Randall May 21 '15 at 22:15