iOS-NSOutputStream不写入数据

时间:2018-07-06 11:44:26

标签: ios objective-c nsoutputstream

我正在下载文件并将其字节写入NSOutputStream

 self.outputFileStream = [NSOutputStream outputStreamToFileAtPath:downloadFilePath 
 append:NO];    
 [self.outputFileStream setDelegate:self];

然后,在NSURLSession的{​​{1}}回调中:

didReceiveResponse

而且,一旦收到第一个字节:

[self.outputFileStream open];

我第一次尝试将数据写入流时,执行停止:const uint8_t *bytes = (const uint8_t*)[data bytes]; NSInteger bytesLeft = [data length]; NSInteger bytesWritten = -1; do { bytesWritten = [self.outputFileStream write:bytes maxLength:bytesLeft]; if (bytesWritten == -1) { break; } bytesLeft -= bytesWritten; } while (bytesLeft > 0); 返回-1。

要弄清楚为什么会发生这种情况,我实现了一个委托方法write:maxLength:

由于某种原因,只有在初始化流时放置以下代码行,它才会被调用:

handleEvent: eventCode:

并返回带有说明[self.outputFileStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; 的{​​{1}},尽管已仔细检查该目录是否存在。

0 个答案:

没有答案