iPhone 执行选择器在后台

发布于 2024-12-17 03:25:05 字数 1750 浏览 4 评论 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];

}

这里我创建一个标签栏控制器并删除启动屏幕。然后启动我的第一个ViewController。

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

编辑:这是位置代码:

- (void) getLocation 
{


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

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



}

I have this problem:
Here is a part of my appDelegate file where I create a "performSelectorInBackground" method.

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

[self addSplash];

[self getLocation];

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

return YES;

}

First I add some splash screen, the I get a location and the call background method.
This is content of background method:

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


}

I download some data, check version of data, setup strings for application and the call on main thread method to create a tab bar controller code here:

- (void) resultOp
{

    tabBarController.delegate = self;


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

    [self removeSplash];

}

Here I create a tab bar controller and remove splash screen. Then start my firstViewController.

Problem is that in my firstViewController I show a current location, but it is wrong. Sometimes is correct but very often is wrong.
Where is a problem ? Is there any option how to check if background thread end ? Or something other solution for my problem (I need only: show splash with activity indicator and some messages (this messages are changed in method e.g. init, get location etc.), then I need get location, the remove splash and show firstViewController) ... thanks a lot

Edit: Here is code for location:

- (void) getLocation 
{


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

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



}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我偏爱纯白色 2024-12-24 03:25:05

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

也许您可以显示您的代码,其中有 CLLocationManager 的事件。

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

Keep in mind, that the longer the location update runs, the more accurate it gets. The first 'hit' for the location isn't always the best one (most of the time it is the wrong one).

Maybe you could show your code where you have the events of the CLLocationManager.

Also, how much is the wrong position off of the right position? I think the AGPS thing first quickly checks its location by using WiFi hotspots nearby and after that gets more accurate by using the GPS chip.

感情洁癖 2024-12-24 03:25:05

无论您在何处使用您获取的位置(我无法从您发布的代码中真正看到),您都应该检查您正在接收的 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
        }
    }

Wherever you are using the location you acquire (which I can't really see from the code you posted), you should have checks on the horizontalAccuracy property of the newLocation you are receiving (and verticalAccuracy as well if altitude matters). You can say something like

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

If you do not do these types of checks, you can get some really inaccurate locations, up to three or four kilometers from your real location at first.

Also, when using multiple threads, data integrity can sometimes become a problem. You need to make sure that variables are not being accessed and changed at the same time in multiple methods, or who knows if you will get the correct output.

Also, it is important to note that all of the methods called within backgroundOp will also be preformed in the background, even without explicitly calling them that way. Use

[self performSelectorOnMainThread:foo withObject:foobar waitUntilDone:NO];

to return to the main thread.

Edit:

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
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文