Question

Im doing a small test project with SocketRocket on ios. But i can't seem to wrap my head around the logic.

Here's the thing: I have need to have a sort of "global" function to call the sockets. When my app opens it should connect using the websockets.

_webSocket.delegate = nil;
[_webSocket close];

_webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"ws://localhost:12345/connectr"]]];
_webSocket.delegate = self;

self.title = @"Opening Connection...";
[_webSocket open];

However, i need to get the logic on connecting to a delegate function. I already thought of a singleton but i read that the singleton functions are quite cpu expensive.

So basically my question is: What is the best way to initiate a global accesable function that uses the (appdelegate) initalized SocketRocket instance.

Was it helpful?

Solution 3

you declare variable in appdelegate with property and synthesis

you import appdelegate class in your requirement class

AppDelegate  *objappdel=(AppDelegate*)[[UIApplication sharedApplication]delegate];  
objappdel.variablename;

OTHER TIPS

You can use a singleton without trouble. Really. It's way better than making your AppDelegate with 50000 LOC. Also, your AppDelegate should know only about app events, not sockets.

I don't know how you assume singletons are cpu expensive. The only expense of singleton is lazily initialising the object once in the first call.

In the other hand, singleton pattern is usually considered a bad practice because of accessing an object statically because you are referring the singleton class instead of a reference to the instance. But it's certainly better to use a singleton instead of accessing the app delegate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top