iphone - Invoking a singleton class to keep a sockets connection open -


i have created singleton class job create tcp socket connection , keep open infinitely while application running. believe singleton class looks it's supposed (with other stackoverflow questions me there) problem not know when or how invoke class object. i'm still pretty new ios development, pretty foreign me.

  • my app has multiple views
  • i want use singleton class create tcp socket connection , keep connection open @ times
  • i not know call/invoke singleton class object
  • i not know how call/invoke singleton class object
  • i using smallsockets library

here class files:

socketconnection.h

#import <foundation/foundation.h>  @interface socketconnection : nsobject {  }  + (socketconnection *)getinstance;  @end 

socketconnection.m

#import "socketconnection.h" #import "imports.h"  static socketconnection *sharedinstance = nil;  @implementation socketconnection  - (id)init {     self = [super init];      if (self)      {         while(1)         {             socket *socket;             int port = 11005;             nsstring *host = @"199.5.83.63";              socket = [socket socket];              @try             {                 nsmutabledata *data;                 [socket connecttohostname:host port:port];                 [socket readdata:data];                 //  [socket writestring:@"hello world!"];                  // connection successful //                 [socket retain]; // must retain if want use out of action block.             }             @catch (nsexception* exception)              {                 nsstring *errmsg = [nsstring stringwithformat:@"%@",[exception reason]];                 nslog(errmsg);                 socket = nil;             }         }     }     return self; }  + (socketconnection *)getinstance {     @synchronized(self)      {         if (sharedinstance == nil)          {             sharedinstance = [[socketconnection alloc] init];         }     }     return sharedinstance; }  @end  

to use singleton, , connection associated it, call getinstance method reference connection trying use. instead of typical alloc/init process. can use getinstance method each of views, , maintain same connection intending.

also, because way create singleton, if call alloc/init instead of getinstance, create new connection instead of using previous one.

an example of how might create reference within view be:

@property (strong, nonatomic) socketconnection * connection -(void) awakefromnib {     connection = [socketconnection getinstance]; } 

code similar cause of views use same connection, not create connection until had view onscreen using connection. create connection @ application startup, add same 'getinstance' 1 of appdelegate methods.

also, because using singleton, , variable never released, may want consider adding methods reinitialize, remove , manage connection server.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -