开发者

Log Accelerometer data to a file on Iphone IOS

Hi I am trying to write to a file from the accelerometer data. Here is my code:

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

//xax.text = [NSString stringWithFormat:@"X:[%2.6f]",acceleration.x];   
//yax.text = [NSString stringWithFormat:@"Y:[%2.6f]",acceleration.y];   
//zax.text = [NSString stringWithFormat:@"Z:[%2.6f]",acceleration.z];

NSString *acc_x = [[NSString alloc] initWithFormat:@"X:[%2.6f]",acceleration.x];
NSString *acc_y = [[NSString alloc] initWithFormat:@"Y:[%2.6f]",acceleration.y];
NSString *acc_z = [[NSString alloc] initWithFormat:@"Z:[%2.6f]",acceleration.z];

xax.text = acc_x;
yax.text = acc_y;
zax.text = acc_z;

[acc_x release];
[acc_y release];
[acc_z release];


//wfm == 2 //Initialization of Appending to the file
if (wfm == 2)
{

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *txtFileName = [[NSString alloc] initWithFormat:@"tmp/%@.txt",txtName.text];
    NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:txtFileName];

    //NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/acc_w_trial2.txt"];

    //Current Contents of the file
    NSString *fileCurrent = [[NSString alloc] initWithContentsOfFile:fileName];

    //Date and Time of each Accelerometer Data
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm:ss:SSS"];
    NSDate *date = [NSDate date];
    NSString *formattedDateString = [dateFormatter stringFromDate:date];

    NSString *msg = [[NSString alloc] initWithFormat:@"%@%@ %@%@%@ \n",fileCurrent,formattedDateString,xax.text,yax.text,zax.text];

    //Convert NSstring to NSData
    NSData* data=[msg dataUsingEncoding: [NSString defaultCStringEncoding]];

    //bool fileCreationSuccess = [fileManager createFileAtPath:fileName contents:data attributes:nil];
    [fileManager createFileAtPath:fileName contents:data attributes:nil];

    [msg release];
    [dateFormatter release];
    [fileCurrent release];
    [txtFileName release];
开发者_如何学JAVA
}   

}

I get the warning level 1 and level 2. Is there a way I can release the NSFileManager memory to prevent this from locking up?


Your handler method to collect accelerometer data seems not very performant. You are allocating the resources (memory, file) everytime which can take a long time. You should allocate the needed resources only once (i.e. use dispatch_once) and keep the file open. Use a NSFileHandle (method fileHandleForWritingAtPath) in order to append the data at the end of the file.

Furthermore NSHomeDirectory() is not where you're supposed to save user data, as iOS apps are sandboxed.

Either use NSTemporaryDirectory() or write in the Documents or Library Folder. The following is from Apple's sample code, usually in application delegate class:

- (NSString *)applicationDocumentsDirectory {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}


You can try using an Autorelease Pool.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜