0

我有这个问题:这是我的 appDelegate 文件的一部分,我在其中创建了一个“performSelectorInBackground”方法。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[self addSplash];

[self getLocation];

[self performSelectorInBackground:@selector(backgroundOp) withObject:nil];

return YES;

}

首先我添加一些启动画面,我得到一个位置和调用背景方法。这是后台方法的内容:

- (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];


}

我下载了一些数据,检查数据版本,为应用程序设置字符串以及调用主线程方法来创建标签栏控制器代码:

- (void) resultOp
{

    tabBarController.delegate = self;


    [self.window addSubview:tabBarController.view];
    [self addTabBarArrow];
    [self.window makeKeyAndVisible];

    [self removeSplash];

}

在这里,我创建了一个标签栏控制器并删除了启动画面。然后启动我的 firstViewController。

问题是在我的 firstViewController 中显示了当前位置,但这是错误的。有时是正确的,但很多时候是错误的。哪里有问题?是否有任何选项如何检查后台线程是否结束?或者解决我的问题的其他方法(我只需要:显示带有活动指示器和一些消息的启动画面(这些消息在方法中发生了更改,例如 init、获取位置等),然后我需要获取位置、删除启动画面并显示 firstViewController)。 .. 多谢

编辑:这是位置代码:

- (void) getLocation 
{


    splashScreenController.splashLabel.text = @"Localization ...";

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kDistanceFilter;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    [locationManager startUpdatingLocation];



}
4

2 回答 2

0

请记住,位置更新运行的时间越长,它就越准确。该位置的第一个“命中”并不总是最好的(大多数时候它是错误的)。

也许你可以在你有 CLLocationManager 事件的地方显示你的代码。

另外,错误的位置与正确的位置相差多少?我认为 AGPS 事物首先通过使用附近的 WiFi 热点快速检查其位置,然后通过使用 GPS 芯片变得更加准确。

于 2011-11-21T12:21:43.690 回答
0

无论您在哪里使用您获得的位置(我无法从您发布的代码中真正看到),您都应该检查您收到的 newLocation 的 Horizo​​ntalAccuracy 属性(如果高度很重要,还应该检查 VerticalAccuracy )。你可以说类似

if(newLocation.horizontalAccuracy < 100) {
    //do something with newLocation
    //because it is accurate to 100 meters
}

如果您不进行这些类型的检查,您可能会得到一些非常不准确的位置,最初距离您的真实位置最多三到四公里。

此外,当使用多线程时,数据完整性有时会成为问题。您需要确保变量不会在多个方法中同时被访问和更改,或者谁知道您是否会得到正确的输出。

此外,重要的是要注意,在 backgroundOp 中调用的所有方法也将在后台执行,即使没有以这种方式显式调用它们。利用

[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
        }
    }
于 2011-11-21T12:28:35.280 回答