NSInputStream przestaje działać, czasami zgłasza EXC_BAD_ACCESS
(AKTUALIZOWANE) w skrócie jest to problem: w iOS chcę odczytać duży plik, wykonać na nim przetwarzanie (w tym konkretnym przypadku zakodować jako łańcuch Base64 () i zapisać do pliku tymczasowego na urządzeniu. Ustawiam NSInputStream na czytać z pliku, a następnie w
(void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
Robię większość pracy. Z jakiegoś powodu czasami widzę, że NSInputStream przestaje działać. Wiem, bo mam linię
NSLog(@"stream %@ got event %x", stream, (unsigned)eventCode);
na początku(void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
a czasami po prostu widziałem wyjście
stream <__NSCFInputStream: 0x1f020b00> got event 2
(co odpowiada zdarzeniu NSStreamEventHasBytesAvailable), a następnie nic później. Nie zdarzenie 10, które odpowiada NSStreamEventEndEnctered, a nie zdarzenie błędu, nic! A czasem nawet dostaję wyjątek EXC_BAD_ACCESS, który w tej chwili nie mam pojęcia, jak debugować. Każda pomoc byłaby doceniana.
Oto implementacja. Wszystko zaczyna się, gdy kliknę przycisk „Prześlij”, który uruchamia:
- (IBAction)submit:(id)sender {
[p_spinner startAnimating];
[self performSelector: @selector(sendData)
withObject: nil
afterDelay: 0];
}
Oto sendData:
-(void)sendData{
...
_tempFilePath = ... ;
[[NSFileManager defaultManager] createFileAtPath:_tempFilePath contents:nil attributes:nil];
[self setUpStreamsForInputFile: [self.p_mediaURL path] outputFile:_tempFilePath];
[p_spinner stopAnimating];
//Pop back to previous VC
[self.navigationController popViewControllerAnimated:NO] ;
}
Oto setUpStreamsForInputFile o nazwie powyżej:
- (void)setUpStreamsForInputFile:(NSString *)inpath outputFile:(NSString *)outpath {
self.p_iStream = [[NSInputStream alloc] initWithFileAtPath:inpath];
[p_iStream setDelegate:self];
[p_iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[p_iStream open];
}
W końcu jest to miejsce, w którym występuje większość logiki:
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
NSLog(@"stream %@ got event %x", stream, (unsigned)eventCode);
switch(eventCode) {
case NSStreamEventHasBytesAvailable:
{
if (stream == self.p_iStream){
if(!_tempMutableData) {
_tempMutableData = [NSMutableData data];
}
if ([_streamdata length]==0){ //we want to write to the buffer only when it has been emptied by the output stream
unsigned int buffer_len = 24000;//read in chunks of 24000
uint8_t buf[buffer_len];
unsigned int len = 0;
len = [p_iStream read:buf maxLength:buffer_len];
if(len) {
[_tempMutableData appendBytes:(const void *)buf length:len];
NSString* base64encData = [Base64 encodeBase64WithData:_tempMutableData];
_streamdata = [base64encData dataUsingEncoding:NSUTF8StringEncoding]; //encode the data as Base64 string
[_tempFileHandle writeData:_streamdata];//write the data
[_tempFileHandle seekToEndOfFile];// and move to the end
_tempMutableData = [NSMutableData data]; //reset mutable data buffer
_streamdata = [[NSData alloc] init]; //release the data buffer
}
}
}
break;
case NSStreamEventEndEncountered:
{
[stream close];
[stream removeFromRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
stream = nil;
//do some more stuff here...
...
break;
}
case NSStreamEventHasSpaceAvailable:
case NSStreamEventOpenCompleted:
case NSStreamEventNone:
{
...
}
}
case NSStreamEventErrorOccurred:{
...
}
}
}
Uwaga: kiedy opublikowałem to jako pierwszy, miałem złe wrażenie, że problem ma coś wspólnego z używaniem GCD. Zgodnie z odpowiedzią Roba poniżej usunąłem kod GCD i problem nadal występuje.