Objective c 我怎么能延迟15秒?
我的程序通过GPS加载用户的当前位置,并将其保存在txt文件中,以便通过电子邮件发送,用于统计目的。我的问题是:Objective c 我怎么能延迟15秒?,objective-c,cocoa,Objective C,Cocoa,我的程序通过GPS加载用户的当前位置,并将其保存在txt文件中,以便通过电子邮件发送,用于统计目的。我的问题是: 首次尝试在每次保存位置时给您15秒的延迟,包括: [NSTimer scheduledTimerWithTimeInterval:15.0目标:自选择器:@selector(locationManager:)用户信息:无重复:是] 问题是这不起作用,也不知道原因是什么 我的另一个问题是:如果文件太大。。。为此,我想命名创建日期和每天不同的日期。我尝试了几件事,但什么也没得到
- 首次尝试在每次保存位置时给您15秒的延迟,包括:
[NSTimer scheduledTimerWithTimeInterval:15.0目标:自选择器:@selector(locationManager:)用户信息:无重复:是]代码>
- 我的另一个问题是:如果文件太大。。。为此,我想命名创建日期和每天不同的日期。我尝试了几件事,但什么也没得到
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//Obtenemos las coordenadas.
latitud = newLocation.coordinate.latitude;
longitud = newLocation.coordinate.longitude;
precision = newLocation.horizontalAccuracy;
// Lo mostramos en las etiquetas
[latitudLabel setText:[NSString stringWithFormat:@"%0.8f",latitud]];
[longitudLabel setText:[NSString stringWithFormat:@"%0.8f",longitud]];
[precisionLabel setText:[NSString stringWithFormat:@"%0.8f",precision]];
tiempoLabel=[self.dateFormatter stringFromDate:newLocation.timestamp];
//NSLog(@"tiempo: %@",tiempoLabel);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"textfile.txt"];
//[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
// create if needed
if (![[NSFileManager defaultManager] fileExistsAtPath:path]){
[[NSData data] writeToFile:path atomically:YES];}
NSString *contents = [NSString stringWithFormat:@"tiempo: %@ latitude:%+.6f longitude: %+.6f precisión: %f\n",tiempoLabel,latitud,longitud,precision];
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:path];
[handle truncateFileAtOffset:[handle seekToEndOfFile]];
[handle writeData:[contents dataUsingEncoding:NSUTF8StringEncoding]];}
谢谢你的帮助。StackOverflow在我的项目中帮了我很多忙您提出了两个不同的问题:
locationManager:
的方法?您尚未展示该方法,因此它看起来非常可疑。如果您试图调用您的位置管理器:didUpdateToLocation:fromLocation:
,则此操作无效。NSTimer
的@选择器
不能有多个参数。您可以编写一个简单的计时器处理程序方法来调用您想要的任何方法,例如
- (void)handleTimer:(NSTimer *)timer
{
// call whatever method you want here
}
然后你可以做:
[NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
请注意,重复计时器可能会导致保留周期,因此,如果您计划在某个时候取消此功能,您可能希望保留对计时器的引用,以便以后可以使其无效(例如在视图中消失)
话虽如此,我同意律师的意见,不要每15秒写一次,而是使用and来控制调用location manager委托方法的时间。每x秒记录一次位置是粉碎用户电池的好方法。有关其他节电指南,请参阅《位置感知编程指南》中的
- 您不需要执行“如果需要创建”代码;及
- 编写文件时,不需要使用
code;您可以只使用NSFileHandle
实例方法(顺便说一下,该方法包含一个NSString
参数,您可以检查文件写入是否成功)NSError
您可能想放弃计时器,继续检查一定数量的精度,以下是一些检查gps精度的示例代码,需要根据您的需要进行修改或从中提取一两行代码
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// test the age of the location measurement to determine if the measurement is cached
// in most cases you will not want to rely on cached measurements
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
// test that the horizontal accuracy does not indicate an invalid measurement
if (newLocation.horizontalAccuracy < 0) return;
// test the measurement to see if it is more accurate than the previous measurement
if (bestEffortAtLocation == nil || bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy) {
// store the location as the "best effort"
self.bestEffortAtLocation = newLocation;
// test the measurement to see if it meets the desired accuracy
//
// IMPORTANT!!! kCLLocationAccuracyBest should not be used for comparison with location coordinate or altitidue
// accuracy because it is a negative value. Instead, compare against some predetermined "real" measure of
// acceptable accuracy, or depend on the timeout to stop updating. This sample depends on the timeout.
//
if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) {
// we have a measurement that meets our requirements, so we can stop updating the location
//
// IMPORTANT!!! Minimize power usage by stopping the location manager as soon as possible.
//
[self stopUpdatingLocation:NSLocalizedString(@"Acquired Location", @"Acquired Location")];
// we can also cancel our previous performSelector:withObject:afterDelay: - it's no longer necessary
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(stopUpdatingLocation:) object:nil];
}
}
}
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation{
//测试位置度量的年龄,以确定度量是否已缓存
//在大多数情况下,您不希望依赖缓存的度量
NSTimeInterval locationAge=-[newLocation.timestamp timeintervalncesInnow];
如果(位置>5.0)返回;
//测试水平精度是否表明测量无效
if(newLocation.horizontalcreaction<0)返回;
//测试测量值,查看其是否比以前的测量值更准确
if(bestEffortAtLocation==nil | | bestEffortAtLocation.HorizontalAccurance>newLocation.HorizontalAccurance){
//将位置存储为“尽最大努力”
self.bestEffortAtLocation=新位置;
//测试测量值,看它是否符合要求的精度
//
//重要提示!!!KCLLOCATIONACURACYBEST不应用于与位置坐标或高程进行比较
//准确度,因为它是一个负值。相反,与一些预先确定的“真实”测量值进行比较
//可接受的准确性,或取决于停止更新的超时。此示例取决于超时。
//
if(newLocation.horizontalAccuracy非常感谢。非常感谢。请帮助我。现在我尝试使用[[NSFileManager defaultManager]removitematpath:path error:nil];
清除文本文件,但我不知道它是否正确。