JavaCake
JavaCake

Reputation: 4115

Threading a TCP Controller in iOS

Today i was testing my project which i have builded in two different controllers, a ViewController and a TCPController. My ViewController instansiates the TCPController (singleton) which updates output and input stream. Now under the test i could determine some lagging on the GUI interface, which was easy to blame the TCPController for.

Is there a best practice on how to thread the TCP controller (client side) before i use the standard tutorial on Apples website: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html

Example on how this would be resolved is very welcome.

Upvotes: 0

Views: 235

Answers (1)

Jody Hagins
Jody Hagins

Reputation: 28419

Reading documentation is always a good idea.

What you do depends in part on your communication framework. Most good frameworks already provide asynchronous methods. If yours does not. Look for something else.

Barring that, in general, you will want to execute your code in a background thread. If it is one long job, then the following should do the trick...

dispatch_queue_t commQ = dispatch_queue_create("some.unique.labe", 0);
dispatch_async(commQ, ^{
    // Now, any code running in this block is running in a different thread.
    // When you get done, and want to talk to the UI, you must use the main
    // queue for any UIKit calls...

    dispatch_async(dispatch_get_main_queue(), ^{
        // Now this code is running on the main queue
        // Do all your UI stuff here...

    });
});
dispatch_release(commQ);

Upvotes: 1

Related Questions