BrianJ
BrianJ

Reputation: 31

iOS xcode poll method at regular intervals

I have written a class that handles communication over a network and I want a method in the ViewController class to watch certain variables in the communication class and update label text when data from the network is received. I tried calling a method at the end of the ViewDidLoad method after making the calls to establish the network communication but that attempt stopped the view from displaying, and monitoring the network I never saw a connection established.

What is the best way for a method in a ViewController class to watch for updates to public member variables in my communicator class?

Upvotes: 1

Views: 535

Answers (3)

Merlevede
Merlevede

Reputation: 8170

You have many options

  • Key Value Observing: You register your View Controller as an observer for a particular property in the other class. Notes: Easy to implement, but your connection class must modify a property that is KVO compliant.
  • Delegation: you can create a protocol, and make your View Controller class be the delegate for the connection class. Notes: A little messy to implement, because you need to create the protocol, make you VC implement the protocol, and create a delegate object in the connection class
  • Notifications: You can register the View Controller to receive a custom notification sent by the connection class. Notes: Easy to implement, and you can register many listeners without problems.
  • Callback Block: You can pass a block from the View Controller to the connection class, and ask it to execute the block. Notes: Very simple and elegant, but blocks can be tricky.

Upvotes: 3

Paulw11
Paulw11

Reputation: 114865

You can also use NSNotificationCenter to advertise changes to other objects in your application

Upvotes: 0

Igor Matyushkin
Igor Matyushkin

Reputation: 778

The best way is to make your view controller to be a delegate for communicator instance.

Upvotes: 0

Related Questions