Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 952545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:54:38+00:00 2026-05-15T23:54:38+00:00

I guess my question stems from ignorance on how network communication is done on

  • 0

I guess my question stems from ignorance on how network communication is done on different threads on the iOS platform. I have a TcpConnection class which is a wrapper for input and output streams to a printer. the Open call looks like this:

    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef) ipAddress, port, &readStream, &writeStream);      
    self.myInputStream = (NSInputStream*)readStream;
    self.myOutputStream = (NSOutputStream*)writeStream;

    CFRelease(readStream);
    CFRelease(writeStream);

    [self.myInputStream setProperty:(id)kCFBooleanTrue forKey:(NSString *)kCFStreamPropertyShouldCloseNativeSocket];
    [self.myInputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [self.myInputStream open];

    [self.myOutputStream setProperty:(id)kCFBooleanTrue forKey:(NSString *)kCFStreamPropertyShouldCloseNativeSocket];
    [self.myOutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [self.myOutputStream open];

The problem is with the [NSRunLoop currentRunLoop] call. When i create this TcpConnection object in a thread which is not the main thread (by using detachNewThreadSelector), I can only communicate with that connection on that thread. If I try to go back to the main thread, or pass it to another thread, I can’t get any data back.

If, however, I schedule the input/output streams in the mainRunLoop it works fine. But that scares the pants off of me. I don’t know what that will do. Does that mean all stream comm is done on the main thread? Are there any other performance issues? I haven’t found any examples where network communications is scheduled on the mainRunLoop; does that mean I shouldn’t do it? Am I using threads incorrectly when I try to talk to my TcpConnection from multiple threads?

I read the Threading Programming Guide but it doesn’t seem to address this issue.
Any help would be appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:54:39+00:00Added an answer on May 15, 2026 at 11:54 pm

    By scheduling on the main run loop, you are telling the streams to perform their delegate callbacks on the main thread. This may or may not be an issue, depending on how your stream callbacks behave.

    I would say that trying to pass your connection objects between threads is a design smell. What I would recommend would be to have a single, long-lived connection thread which runs its own run loop. Schedule your streams on that thread’s run loop, then pass messages between it and other threads to shuffle data onto or off of the network. This will keep your network communication nicely localized to a single thread/run loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HashMap (although I guess this question applies to other collections) of
Ok another WPF question, well I guess this is just general .NET. I have
I guess the real question is: If I don't care about dirty reads, will
Sorry for the long question title. I guess I'm on to a loser on
My guess is that class variables (class var) are truly global in storage (that
For example: http://stackoverflow.com/questions/396164/exposing-database-ids-security-risk and http://stackoverflow.com/questions/396164/blah-blah loads the same question. (I guess this is DB
Umm, I guess my questions in the title: How do I turn on Option
I guess this is an odd one, and the answer is most likely it
I guess I'll illustrate with an example: In this game you are able to
I guess I'm really after an aid to help people who forget, Cruise Control

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.