ios - NSStream 关​​闭得太快

标签 ios multithreading nsstream

我在 iOS 中进行客户端-服务器通信已经有一段时间了,但我遇到了一个我无法理解的问题。

我写了两个基本函数:一个向服务器发送数据,另一个从服务器接收数据。每个都有一个名为 timeout 的参数,它允许当前线程 sleep 并每 0.25 秒唤醒一次,直到达到超时:

-(ReturnCode) send : (NSData*)data :(int)timeOut
   {
    if(![self isConnectionOpened]) return KO;

    float timer = 0;

    while(![_outputStream hasSpaceAvailable])
    {
        [NSThread sleepForTimeInterval:0.25];
        timer+=0.25;
        if(timer == timeOut) break;
    }

    int ret = 0;
    if([_outputStream hasSpaceAvailable]){
        int lg = [data length];

        ret = [_outputStream write:[data bytes] maxLength:lg];

        if(ret == -1) return KO;
        else return OK;
    }

    return TIMEOUT;
}

- (ReturnCode) receive : (NSData**)data : (int)timeOut : (int)maxLength
{
    uint8_t buffer[maxLength];
    int len;
    NSMutableData* dataReceived = [[NSMutableData alloc] init];
    if(! [self isConnectionOpened]) return KO;

    float timer = 0;
    while(![_inputStream hasBytesAvailable])
    {
        [NSThread sleepForTimeInterval:0.25];
        timer+=0.25;
        if(timer == timeOut) break;
    }

    if(![_inputStream hasBytesAvailable]) return TIMEOUT;

    while ([_inputStream hasBytesAvailable]) {
        len = [_inputStream read:buffer maxLength:sizeof(buffer)];
        if (len > 0) {
            [dataReceived appendBytes:buffer length:len];
            *data = dataReceived;
            return OK;
        }
    }
    return KO;

}

使用 iPhone 4 + iOS6,一切都很顺利。但在 iOS7 下,由于某些模糊原因,输入流和输出流过早关闭(引发 NSStreamEventErrorOccurred)。事实是,如果我在从服务器接收数据之前放置一个断点并使代码运行,它可以正常工作并且读取/写入流不会错误关闭。

所以我认为存在同步问题,但我不明白为什么...如果有人有想法,请帮助...

最佳答案

我找到了问题的根源。

实际上,要非常小心安排输入流和输出流的位置。事实上,我被告知 Apple 对象必须在主线程上执行,所以我这样安排它们:

dispatch_async(dispatch_get_main_queue(), ^ {
     [_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
     [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

     [_inputStream open];
     [_outputStream open];
});

但实际上,最好将它们调度到当前线程的当前循环上,而不是在主线程上调度调度操作:

     [_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
     [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

     [_inputStream open];
     [_outputStream open];

关于ios - NSStream 关​​闭得太快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20165314/

相关文章:

java - ScheduledExecutorService 未正确触发

objective-c - 当 NSStream 失去连接时如何重新连接?

ios - NSOutputStream 总是写入零字节

iphone - 尝试从导航堆栈中弹出 View 时断言失败

ios - 在多点触控中跟踪触摸点

python - 树莓派上的Python线程问题

java - 在 Quasar 中调度任务,无需生成新线程

iphone - CCSpeed延时后的Cocos2D调用函数

ios - NSURLSession 后台下载不工作

objective-c - 通过 NSOutputStream 发送 UIImage