1

In my main view class, i have an instance of another class that is a connection via tcp socket.

When i leave that view , and than back, i don't want to free/lose this instance because i DONT want to make a new connection , it must stay the same (socket is like a chat-always connected)

        socketInstance=[[SocketConnector alloc]init];
        socketInstance.delegate=self;

This is done at the init of the view , only at first launch. than, when i am leaving the view and come back (if i don't perform this lines again), will he save the socketInstance reference for me ? if not, how would i make him save it for the rest of the program lifetime ? I cant create this instance again and again .

danielbeard
  • 9,120
  • 3
  • 44
  • 58
Curnelious
  • 1
  • 16
  • 76
  • 150

1 Answers1

1

could you use a static variable? otherwise use a singleton pattern that you can keep global

Community
  • 1
  • 1
Fonix
  • 11,447
  • 3
  • 45
  • 74
  • Thanks. how would i use static for this? and about the singleton, dont forget that i have to get the delegates to the main view .. – Curnelious Mar 27 '13 at 07:45
  • the singleton of the `SocketConnector` can still keep delegates surely... im not sure what you mean – Fonix Mar 27 '13 at 07:50
  • i would personally go with [this answer](http://stackoverflow.com/a/145164/1219956) can include the header in your projectname-Prefix.pch file and then you will have access to the static function that will return that instance of the object where ever you call it – Fonix Mar 27 '13 at 08:03
  • Thanks it seems the right way but i am little bit scare because i am not sure how to exactly implement it- and insert the scoket class and delegates to it , without mistakes .. – Curnelious Mar 27 '13 at 08:14
  • 1
    its basically the same as any other class, except instead of going `classname *instance = [[classname alloc] init];` you are just going `classname *instance = [classname sharedSingleton];`. you are just adding that one method to your class, and including the header globally, thats the only change you need to make. if the socket class is not yours, then just subclass/add a category that will implement that method. – Fonix Mar 27 '13 at 08:22
  • ok thanks , so if i put the socketClass instance in the singleton , how would i use the scoketClass methods ? i mean, till now i did : [socketInstance writeData:name] , so now how would i get to that instance and use his classes ? – Curnelious Mar 27 '13 at 09:12
  • exactly the same, just to get the instance of it, first you go `SocketClass *socketInstance = [SocketClass sharedSingleton]; [socketInstace writeData:name];` all a singleton is, is just a way of making sure only 1 instance of an object exists, and if it doesnt exist yet, it creates it. – Fonix Mar 27 '13 at 09:17
  • thanks a lot. i wanted to create another singleton outside the socketclass that will create an instance of the socketclass, but than i didnt know where to set the delegate- in the singleton or on the other class that use it (i.e. on the other class that use the singleton: [socketManager sharedManager].scoketInstance.delegate=self; ) – Curnelious Mar 27 '13 at 09:30
  • probably easiest to set in the other class since you will just be able to go `socketInstance.delegate=self;` like how you had it. just might want to do a check if the delegate is already assigned `if(socketInstance.delegate == nil)`, if that matters to you – Fonix Mar 27 '13 at 09:35