NSStream and Sockets, NSStreamDelegate methods not being called

后端 未结 2 1782
礼貌的吻别
礼貌的吻别 2020-12-07 18:03

I\'ve followed the guide Setting Up Socket Streams and have effectively duplicated that code in my class. No matter what I try the delegate methods just don\'t seem to get c

2条回答
  •  臣服心动
    2020-12-07 18:49

    In the lines like this:

    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    

    Instead of using [NSRunLoop currentRunLoop] I changed it to [NSRunLoop mainRunLoop].

    EDIT 2011-05-30:

    The reason this did not work is because I was setting up the sockets in a background thread via +[NSThread detachNewThreadSelector:toTarget:withObject:].

    Doing it that way created a new run loop, which after reading the run loop developer documentation I discovered that you need to tell the NSRunLoop to run manually.

    Running it in the same run loop as the main thread was fine on performance, though I was able to squeeze a bit more performance out by writing a wrapper class and running all network I/O on a background thread.

提交回复
热议问题