1

I've a ViewController where I call a method from another class (TCP class), where I make a TCP connection to a server, that gives me a response. And I want to, when that TCP class, get's the response from the server, call another method from the ViewController.

Problems:

  1. I'm a noob.
  2. I'm initializing and allocating that first Viewcontroller on the TCP, and all my vars are reseted (something that I don't want).

So... What can I do to make it right? I just want to call a method from a different class, that is already allocated in memory.

Tks!

Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
88fsantos
  • 393
  • 1
  • 7
  • 22

1 Answers1

1

You could set up the ViewController as an observer to the TCP class. This is a link that explains an implementation of the observer pattern in Obj-C. (Very similar to what I use but in a nice write up.)

http://www.a-coding.com/2010/10/observer-pattern-in-objective-c.html

I usually like to separate the persistence layer from the interface as well. I use observers or KVO to notify my business logic and view controllers that something changed.

You can also send the information through the Notification Center that is provided if you prefer...

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html

Basic Code Example:

@implementation ExampleViewController
//...
- (void)viewDidLoad
{
   [super viewDidLoad:animated];
   [TCPClass subscribeObserver:self];
}
- (void)viewDidUnload
{
   [super viewDidUnload:animated];
   [TCPClass unsubscribeObserver:self];
}
- (void)notifySuccess:(NSString*)input
{
   //Do whatever I needed to do on success
}
//...
@end

@implementation TCPClass
//...
//Call this function when your TCP class gets its callback saying its done
- (void)notifySuccess:(NSString*)input 
{    
    for( id<Observer> observer in [NSMutableArray arrayWithArray:observerList] )
    {
        [(NSObject*)observer performSelectorOnMainThread:@selector(notifySuccess:)   withObject:input waitUntilDone:YES];
    }
}
//maintain a list of classes that observe this one
- (void)subscribeObserver:(id<Observer>)input {
    @synchronized(observerList) 
    {
        if ([observerList indexOfObject:input] == NSNotFound) {
        [observerList addObject:input];
        }
    }
}

- (void)unsubscribeObserver:(id<Observer>)input {
    @synchronized(observerList) 
    {
        [observerList removeObject:input];
    }
}
//...
@end

//Observer.h
//all observers must inherit this interface
@protocol Observer
- (void)notifySuccess:(NSString*)input;
@end

Hope that helps!

Kibitz503
  • 867
  • 6
  • 10
  • Tks a lot man. So I have to make my ViewController as an observer of TCP class. It means that all methods, from the TCP class are accessible from the VC? – 88fsantos Jul 18 '12 at 19:24
  • With the observer pattern you are defining an interface and guaranteeing that all observers will have this interface. Traditionally this was a notify() method. When notify is called you then call whatever functions you needed. This allows the view controller to be event driven on the callback from the TCP connection. You of course could also pass the TCP class a pointer to the ViewController then call a specific function on the ViewController after you get the callback but the Observer patter is cleaner, scales better (you can use it for all of your view controllers) and loosely coupled. – Kibitz503 Jul 18 '12 at 19:27
  • 1
    So to answer your question more directly... No. It will be able to see all of the TCP classes public methods though. (I am assuming that the view controller is creating the TCP class) The methods to subscribe and unsubscribe will need to be public in order to use them. – Kibitz503 Jul 18 '12 at 19:51
  • In the TCP class place the function subscribeObserver then be sure to put the prototype in the .h file. You may want to double check my spelling. You can also rename these functions whatever you wish. – Kibitz503 Jul 19 '12 at 21:31
  • Well I real something more and learned how i works, and now it works! Tks a lot for the help! – 88fsantos Jul 20 '12 at 09:15
  • Hey that's great man :) The use of the delegate patter also works here but the observer is a great way to have multiple view controllers monitor changes in the back end. So it scales much better for this! – Kibitz503 Jul 20 '12 at 16:42