我使用“SimpleFTPSample”来执行 ftp 请求以列出错误信息。部分代码如下:
- (void)_startReceive
{
......
self.networkStream = (NSInputStream *) ftpStream;
self.networkStream.delegate = self;
[self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[NSTimer scheduledTimerWithTimeInterval:TIMEOUTFTP target:self
selector:@selector(dealTimeOut:) userInfo:nil repeats:NO];
......
}
我还设置了一个 NSTimer 以在 TIMEOUTFTP 之后停止 networkStream,如果请求在我定义的 TIMEOUTFTP 内正确完成,networkStream 也将关闭。我在另一个地方使用该方法,如下所示:
- (void)downLoadData
{
NSArray* receivedData;
[ftpService _startReceive];
//wait until the network is closed
while (ftpService.isReceiving) {}
receivedData = ftpService.dataArray;
if([receivedData count] == 0) {
NSLog(@"no data get");
}
else {
NSLog(@"get data number %d", [receivedData count]);
}
}
情况是程序卡在“while (ftpService.isReceiving) {}”。我不熟悉多线程。可能是我不正确理解运行循环。有人可以告诉我为什么会发生这种情况以及如何实现我的目的吗?