データが比較的高速 (毎秒 200 回) で受信される場合に、iPhone でカンマ区切りのログ ファイルを作成する方法の提案/実装を探しています。各データ ポイントのタイムスタンプと 2 ~ 3 個の整数をキャプチャすることを期待しています。15 分間で、15*60*200 = 180,000 行のデータがあり、それぞれにタイムスタンプ、いくつかの整数、および改行文字があります。
このデータのディスクへの書き込みが正しい順序で行われるようにしたいと考えています。
私の現在の実装は、1 秒あたり 1 データ ポイントで受信するデータ用に最適化されており、「高速」データにはあまり効率的ではない可能性があります。コードを微調整して、書き込みごとにリソースをあまり消費せずにバックグラウンド スレッドで実行できるようにするにはどうすればよいですか? あるいは、数字を指定して後でログファイルを要求できる高速な「データファイルへのログ」実装はありますか?
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]];
}
}