NSInputStream in blocking mode

Discussion in 'iOS Programming' started by arnieterm, Dec 29, 2009.

  1. arnieterm macrumors regular

    Aug 28, 2008
    For my iphone application that isusing NSStream. I need to use NSInputStream in UI blocking mode so that till the input stream arrives the UI remains blocked as this is my requirement. Though I have scheduled a runLoop to handle the various event types and I am getting these events too. The only problem is that I am not able to block the UI during the receipt.
    How can I make NSInputStream/NSOutputStream as synchronous?.
    The problem is that at the touch of a UIButton I initiates a streaming call and there is a UINavigationController being pushed at the end of the UIButton touch event code. But I receive the streaming event later and navigationController gets pushed. Instead of this I need to wait for stream to come and then push the navigation controller after that
  2. robj macrumors regular

    Dec 23, 2009
    Madrid, Spain
    I'm not sure if it does fit you, but take a look to:


    For example:
    [self performSelectorOnMainThread:@selector(methodName) withObject:argument waitUntilDone:YES]
    This should invoke that method in the main thread, making it a blocking call.
  3. arnieterm thread starter macrumors regular

    Aug 28, 2008
    Thanks for the reply.
    I have already given this method a try but my

    - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
    is not getting called. In the UIButton touch event I have written this.
    [NSThread detachNewThreadSelector: @selector(loginThread) toTarget: self withObject: nil];
    In the loginThread method just above [pool release] call I have used the code as mentioned in your reply but that handler does not gets called.

    Any other thing to use instead?
  4. arnieterm thread starter macrumors regular

    Aug 28, 2008
    The simplest way I so far is to use the below given thing
    [iStream setDelegate: nil];
    [oStream setDelegate: nil];
    After this we can use a while loop to check for if the output screen has space available and input screen for has bytes available
  5. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Move the pushViewController call from within the button code to within the NSInputStream receiving delegate method.
  6. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    Don't block the UI. You are trying to do too much inside one event handler. Split it up into multiple handlers. Have another event handler, notification receiver or delegate push the view after everything that is needed is ready.

Share This Page