I'm looking for suggestions/implementations of how to create a comma separated log file on an iPhone when my data comes at a relatively fast rate (200 times per second). I expect to capture the timestamp and 2-3 integer numbers for each data point. Over a 15 minute period, there would be 15*60*200 = 180,000 rows of data, each one having a timestamp, a few integers and a newline character.
I want to make sure that writing of this data to disk happens in the correct sequential order.
My current implementation has been optimized for data coming in at 1 data point per second, and may not be very efficient for "fast" data. How can I tweak my code to make sure that it can run in a background thread without taking too much resources for each write? Alternatively, is there a fast "log to data file" implementation out there that I can just give numbers to and ask it for a log file at a later point?
NSString *appDataFile ;
NSFileHandle *aFileHandle;
-(NSString*)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
appDataFile = [documentsDirectory stringByAppendingPathComponent:@"Data"];
aFileHandle = [NSFileHandle fileHandleForWritingAtPath:appDataFile];
return appDataFile;
}
//creates a new log file for each app run, or appends to existing log
-(void)writeStringToDataFile:(NSString *)csvLine
{
if(aFileHandle)
{
//telling aFilehandle what file write to
[aFileHandle truncateFileAtOffset:[aFileHandle seekToEndOfFile]]; //setting aFileHandle to write at the end of the file
[aFileHandle writeData:[csvLine dataUsingEncoding:NSUTF8StringEncoding]];
}else{
NSData* headers = [@"timestamp,waveform amplitude,score, connection, event\n" dataUsingEncoding:NSUTF8StringEncoding];
//clear the old log file
NSError *error = nil;
NSFileManager* fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:[self dataFilePath] error:&error];
//create CSV headers
aFileHandle = [NSFileHandle fileHandleForWritingAtPath:appDataFile];
[headers writeToFile:appDataFile atomically:YES];
aFileHandle = [NSFileHandle fileHandleForWritingAtPath:appDataFile];
//telling aFilehandle what file write to
[aFileHandle truncateFileAtOffset:[aFileHandle seekToEndOfFile]]; //setting aFileHandle to write at the end of the file
[aFileHandle writeData:[csvLine dataUsingEncoding:NSUTF8StringEncoding]];
}
}