0

我正在使用苹果 QRunLoopOperation

https://developer.apple.com/library/mac/samplecode/PictureSharing/Listings/QRunLoopOperation_m.html

结合 NSStream 文件下载:

https://developer.apple.com/library/ios/samplecode/SimpleFTPSample/Listings/Read_Me_About_SimpleFTPSample_txt.html

我创建的运行循环的线程是 ASNetworking 风格:

do
{
    @autoreleasepool {
        [[NSThread currentThread] setName:@"FancyThreadName"];

        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
        [runLoop run];
    }
}
while (YES);

这在大多数情况下都可以正常工作,但有时我会收到一个“请求(0x1cd74ad0),而不是当前请求(0x0),表明它在连接 0x1cd48430 上已完成”当然具有不同的对象编号。运行循环本身看起来像

(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {(这里是一个尖锐的符号)pragma used(aStream) assert(aStream == self.networkStream);

switch (eventCode) {
    case NSStreamEventOpenCompleted: {
      // I do something - update state etc
    } break;
    case NSStreamEventHasBytesAvailable:
        // I Handle the bytes and CLOSE (like in the Apple example -> IS THIS BAD?)
        [self handleBytesAvaialble]:// i can perform [self finishWithError:nil] here
        break;
    case NSStreamEventHasSpaceAvailable: {
        assert(NO);     // should never happen 
    } break;
    case NSStreamEventErrorOccurred: {
        //I finish the whole operation and log the error 
        [self finishWithError: someErrorICreate];
    }
        break;
    case NSStreamEventEndEncountered: {
      //This seems to happen before the error 


    } break;
    default: {
        assert(NO);
    } break;
}    

}

好的,有人可以帮我解决这个问题吗?我的想法是:

  1. 也许我不应该以处理可用字节结束,而是以 NSStreamEventEndEncountered 结束?
  2. 也许错误是因为我在 handleBytesAvailable 中完成了操作并且结束事件已安排运行,然后我开始另一个操作并得到错误?

任何帮助表示赞赏。

4

0 回答 0