我有这个问题:这是我的 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];
}