I have to read a very large file into memory (data processing while reading it is not an option, I have to have the entire file in the device memory). When the device runs out of memory, I should stop the reading and display an error message to the user.
- (void)setUpStreamForFile:(NSString *)path {
_inputStream = [[NSInputStream alloc] initWithFileAtPath:path];
[_inputStream setDelegate:self];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
_didReceiveMemoryWarning = YES;
}];
[_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_inputStream open];
}
In my stream delegate method I am checking the _didReceiveMemoryWarning
variable every time, and close the stream if it becomes true.
...
if (!_didReceiveMemoryWarning) {
if(!_tempData) {
_tempData = [NSMutableData data];
}
uint8_t buf[1024];
unsigned int len = 0;
len = [(NSInputStream *)stream read:buf maxLength:1024];
if(len) {
[_tempData appendBytes:(const void *)buf length:len];
}
} else {
[self closeInputStream];
NSError *error error = [NSError errorWithDomain...];
[[NSNotificationCenter defaultCenter] postNotificationName:@"reading failed" object:error];
_didReceiveMemoryWarning = NO;
}
...
- (void)closeInputStream {
[_inputStream close];
[_inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
_inputStream = nil;
_tempData = nil;
}
The reading works on the simulator because I have sufficient memory there, however on the device the OS seems to kill the app before I receive the notification (With smaller files it works on the device as well). Does anyone know a solution to this problem?