Should I have separate thread in ntework-intensive application?

Discussion in 'iOS Programming' started by beachdog, Oct 27, 2008.

  1. beachdog macrumors member

    Aug 10, 2008
    I am building an application which will have a TCP socket connection to a server, over which a large number of messages will be exchanged. Many of the messages will be pushed from the server, and will need to update the UI. I am wondering if in a very network-intensive application such as this, whether I should use NSThread to create a separate runloop in which to do my network message processing, in order to keep it separate from the main thread that is handling the UI updates and other things. I guess I'm wondering if the UI is going to get sluggish when I amd processing large incoming messages. I tried searching the docs for some guidance/best practices on when to create different threads but didn't find any. Any advice from those who might have done something similar would be appreciated. Thx in advance
  2. jnic macrumors 6502a

    Oct 24, 2008
    Yes; run your connections in a separate thread, or connect asynchronously. Otherwise you'll find your UI locking up whilst the connection blocks.

    To update the UI from another thread you'll need to call a method on the main thread from your connection, something like:

    [self performSelectorOnMainThread:@selector(yourMethodThatUpdatesTheUI:)
    withObject:yourObjectFromYourConnection waitUntilDone:NO];

Share This Page