ios – NSInputStream停止运行,有时会抛出EXC_BAD_ACCESS

前端之家收集整理的这篇文章主要介绍了ios – NSInputStream停止运行,有时会抛出EXC_BAD_ACCESS前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
(更新)这是一个简单的问题:在iOS中,我想要读取一个大文件,对它进行一些处理(在这种特殊情况下编码为Base64 string()并保存到设备上的临时文件,我设置一个从文件读取的NSInputStream,然后在
  1. (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode

我在做大部分的工作.由于某些原因,有时我可以看到NSInputStream刚停止工作.我知道,因为我有一条线

  1. NSLog(@"stream %@ got event %x",stream,(unsigned)eventCode);

在(void)stream:(NSStream *)的开头处理streamEvent:(NSStreamEvent)eventCode,有时候我会看到输出

  1. stream <__NSCFInputStream: 0x1f020b00> got event 2

(对应于事件NSStreamEventHasBytesAvailable),然后再没有.不是事件10,对应于NSStreamEventEndEnmitted,不是错误事件,没有!还有时候我甚至得到一个EXC_BAD_ACCESS异常,我现在不知道如何调试.任何帮助将不胜感激.

这是实现.当我点击“提交”按钮时,一切都会启动,这会触发:

  1. - (IBAction)submit:(id)sender {
  2. [p_spinner startAnimating];
  3. [self performSelector: @selector(sendData)
  4. withObject: nil
  5. afterDelay: 0];
  6. }

这里是sendData:

  1. -(void)sendData{
  2. ...
  3. _tempFilePath = ... ;
  4. [[NSFileManager defaultManager] createFileAtPath:_tempFilePath contents:nil attributes:nil];
  5. [self setUpStreamsForInputFile: [self.p_mediaURL path] outputFile:_tempFilePath];
  6. [p_spinner stopAnimating];
  7. //Pop back to prevIoUs VC
  8. [self.navigationController popViewControllerAnimated:NO] ;
  9. }

这里是上面提到的setUpStreamsForInputFile:

  1. - (void)setUpStreamsForInputFile:(NSString *)inpath outputFile:(NSString *)outpath {
  2. self.p_iStream = [[NSInputStream alloc] initWithFileAtPath:inpath];
  3. [p_iStream setDelegate:self];
  4. [p_iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
  5. forMode:NSDefaultRunLoopMode];
  6. [p_iStream open];
  7. }

最后,这是大多数逻辑发生的地方:

  1. - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
  2.  
  3. NSLog(@"stream %@ got event %x",(unsigned)eventCode);
  4.  
  5. switch(eventCode) {
  6. case NSStreamEventHasBytesAvailable:
  7. {
  8. if (stream == self.p_iStream){
  9. if(!_tempMutableData) {
  10. _tempMutableData = [NSMutableData data];
  11. }
  12. if ([_streamdata length]==0){ //we want to write to the buffer only when it has been emptied by the output stream
  13. unsigned int buffer_len = 24000;//read in chunks of 24000
  14. uint8_t buf[buffer_len];
  15. unsigned int len = 0;
  16. len = [p_iStream read:buf maxLength:buffer_len];
  17. if(len) {
  18. [_tempMutableData appendBytes:(const void *)buf length:len];
  19. NSString* base64encData = [Base64 encodeBase64WithData:_tempMutableData];
  20. _streamdata = [base64encData dataUsingEncoding:NSUTF8StringEncoding]; //encode the data as Base64 string
  21. [_tempFileHandle writeData:_streamdata];//write the data
  22. [_tempFileHandle seekToEndOfFile];// and move to the end
  23. _tempMutableData = [NSMutableData data]; //reset mutable data buffer
  24. _streamdata = [[NSData alloc] init]; //release the data buffer
  25. }
  26. }
  27. }
  28. break;
  29. case NSStreamEventEndEncountered:
  30. {
  31. [stream close];
  32. [stream removeFromRunLoop:[NSRunLoop currentRunLoop]
  33. forMode:NSDefaultRunLoopMode];
  34. stream = nil;
  35. //do some more stuff here...
  36. ...
  37. break;
  38. }
  39. case NSStreamEventHasSpaceAvailable:
  40. case NSStreamEventOpenCompleted:
  41. case NSStreamEventNone:
  42. {
  43. ...
  44. }
  45. }
  46. case NSStreamEventErrorOccurred:{
  47. ...
  48. }
  49. }
  50. }

注意:当我第一次发布时,我的错误印象是这个问题与使用GCD有关.根据Rob在下面的回答,我删除了GCD代码,问题依然存在.

解决方法

首先:在你的原始代码中,你没有使用后台线程,而是主线程(dispatch_async,但是在主队列中).

当您安排NSInputStream运行在默认的runloop(因此,主线程的runloop)时,主线程处于默认模式(NSDefaultRunLoopMode)时会收到事件.

但是,如果您检查,在某些情况下(例如,在UIScrollView滚动和其他UI更新期间),默认运行环境更改模式.当主runloop处于与NSDefaultRunLoopMode不同的模式时,不会收到您的事件.

您的旧代码与dispatch_async几乎相当(但是在主线程上移动UI更新).您只需添加一些更改:

>在后台调度,这样的东西:

  1. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
  2. dispatch_async(queue,^{
  3. // your background code
  4.  
  5. //end of your code
  6.  
  7. [[NSRunLoop currentRunLoop] run]; // start a run loop,look at the next point
  8. });

>在该线程上启动一个运行循环.必须使用此代码在调度异步调用的最后一行(最后一行)完成此操作

  1. [[NSRunLoop currentRunLoop] run]; // note: this method never returns,so it must be THE LAST LINE of your dispatch

尝试让我知道

编辑 – 添加示例代码

为了更清楚,我复制粘贴您的原始代码更新:

  1. - (void)setUpStreamsForInputFile:(NSString *)inpath outputFile:(NSString *)outpath {
  2. self.p_iStream = [[NSInputStream alloc] initWithFileAtPath:inpath];
  3. [p_iStream setDelegate:self];
  4.  
  5. // here: change the queue type and use a background queue (you can change priority)
  6. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
  7. dispatch_async(queue,^ {
  8. [p_iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
  9. forMode:NSDefaultRunLoopMode];
  10. [p_iStream open];
  11.  
  12. // here: start the loop
  13. [[NSRunLoop currentRunLoop] run];
  14. // note: all code below this line won't be executed,because the above method NEVER returns.
  15. });
  16. }

进行此修改后,您的

  1. - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {}

方法,将在您启动运行循环的同一线程上调用一个后台线程:如果需要更新UI,重要的是再次发送到主线程.

额外信息:

在我的代码中,我在随机后台队列中使用dispatch_async(在一个可用的后台线程上调度你的代码,或者如果需要的话可以启动一个新的代码),所有这些都是“自动的”).如果您愿意,您可以启动自己的线程,而不是使用dispatch异步.

此外,在发送“运行”消息之前,我不检查一个运行循环是否已经运行(但是您可以使用currentMode方法检查它,查看NSRunLoop参考以获取更多信息).它不应该是必要的,因为每个线程只有一个关联的NSRunLoop实例,所以发送另一个运行(如果已经运行)没有什么不好:-)

你甚至可以避免直接使用runLoops,并使用dispatch_source切换到一个完整的GCD方法,但是我从未直接使用它,所以我现在不能给你一个“好的示例代码

猜你在找的iOS相关文章