iPhone性能选择背景
我有一个问题: 这里是我的appDelegate文件的一部分,我在其中创建了一个“performSelectorInBackground”方法 首先,我添加了一些启动屏幕,我得到了一个位置和调用后台方法。 这是背景法的内容:iPhone性能选择背景,iphone,objective-c,multithreading,geolocation,performselector,Iphone,Objective C,Multithreading,Geolocation,Performselector,我有一个问题: 这里是我的appDelegate文件的一部分,我在其中创建了一个“performSelectorInBackground”方法 首先,我添加了一些启动屏幕,我得到了一个位置和调用后台方法。 这是背景法的内容: - (void) backgroundOp { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; [self createEditableCopyOfDatabaseIfNeeded]
- (void) backgroundOp
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self createEditableCopyOfDatabaseIfNeeded];
[self initTempData];
[self initApp];
[self checkDataVersion];
}
[self setAppStrings];
[self performSelectorOnMainThread:@selector(resultOp) withObject:nil waitUntilDone:YES];
[pool release];
}
我下载了一些数据,检查了数据的版本,为应用程序设置了字符串,并调用了main thread方法,在这里创建了一个选项卡条控制器代码:
- (void) resultOp
{
tabBarController.delegate = self;
[self.window addSubview:tabBarController.view];
[self addTabBarArrow];
[self.window makeKeyAndVisible];
[self removeSplash];
}
在这里,我创建了一个选项卡栏控制器并删除了初始屏幕。然后启动我的第一个ViewController
问题是,在我的firstViewController中,我显示了一个当前位置,但它是错误的。有时是正确的,但往往是错误的。
哪里有问题?有没有办法检查后台线程是否结束?或者针对我的问题的其他解决方案(我只需要:使用活动指示器显示飞溅和一些消息(这些消息在方法中更改,例如init、get location等),然后我需要get location、remove splash和show firstViewController)。。。非常感谢
编辑:以下是位置代码:
- (void) getLocation
{
splashScreenController.splashLabel.text = @"Localization ...";
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kDistanceFilter;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
}
请记住,位置更新运行的时间越长,它就越准确。该位置的第一个“命中率”并不总是最好的(大多数情况下是错误的) 也许您可以将代码显示在CLLocationManager事件所在的位置
另外,错误的位置偏离正确的位置有多大?我认为AGPS首先使用附近的WiFi热点快速检查其位置,然后使用GPS芯片更精确。无论您在哪里使用您获取的位置(我从您发布的代码中看不到),您都应该检查您接收到的新位置的HorizontalAccurance属性(如果高度很重要,垂直精度也一样)
if(newLocation.horizontalAccuracy < 100) {
//do something with newLocation
//because it is accurate to 100 meters
}
返回到主线程
编辑:
viewDidLoad{
[超级视图下载];
迭代次数=-5;
}
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation)
*)newLocation fromLocation:(CLLocation*)oldLocation{
迭代++;
如果(迭代次数>0){
if(新位置水平精度<50){
//用不确定半径处理位置
//50岁以下
}
}
您没有显示用于接收和评估位置的代码,而只显示与您的问题完全无关的代码。我认为解决方案是检查后台方法是否完成。但我不知道如何检查它…但当我在主线程上尝试执行此方法时(不在后台)一切都正常且良好…我尝试以下方法:[自执行selectoronmainthread:foo with object:foobar waitUntilDone:NO];但没有任何变化……我不是说这将解决您的问题。我只是说这是需要注意的。您的问题似乎是,您正在使用的前两个位置更新非常不准确。通过使用我向您展示的更改(等待使用位置的方法,直到获得足够的精度,然后才能获得所需的精度。是的,问题正如您所说的。应用程序启动得非常快,但收到的位置不准确。我的解决方案:首先调用getLocation方法,然后调用[NSThread sleepForTimeInterval:2.0]。然后我得到了正确的位置。我不会这样做。尝试在头文件(int)中创建一个计数器变量(称为迭代次数或其他)并在viewDidLoad方法中将其初始化为-5。在locationManager:didUpdateToNewLocation:fromOldLocation:,增加迭代次数。然后仅在迭代次数大于0时才开始使用newLocations。这比在主线程上使用NSThread sleepThreadForTimeInterval安全得多。
if(newLocation.horizontalAccuracy < 100) {
//do something with newLocation
//because it is accurate to 100 meters
}
[self performSelectorOnMainThread:foo withObject:foobar waitUntilDone:NO];
viewDidLoad {
[super viewDidLoad];
iterations = -5;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
*)newLocation fromLocation:(CLLocation *)oldLocation {
iterations++;
if(iterations > 0) {
if(newLocation.horizontalAccuracy < 50) {
//do something with location with radius of uncertainty
//of less than 50
}
}