如何同步获取位置管理器的共享实例
How to get shared instance of location manger in synchronize
嗨,我想获取后台位置更新,所以我编写了以下代码并使其同步以防止多线程。所以我有两个问题:
我真的需要同步这段代码吗,我在App里做的只是运行后台任务?我从未创建任何特殊的 NSThread
类型来支持多线程并且不需要这样?
每当我需要开始位置更新时,我这样调用:
CLLocationManager *locationManager = [LocationTracker sharedLocationManager];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
是正确的调用方式吗?
+ (CLLocationManager *)sharedLocationManager {
static CLLocationManager *_locationManager;
@synchronized(self) {
if (_locationManager == nil) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[_locationManager requestAlwaysAuthorization];
_locationManager.distanceFilter = 10;
if(IS_OS_9_OR_LATER){
_locationManager.allowsBackgroundLocationUpdates = YES;
}
}
}
return _locationManager;
}
应该同步共享实例。您可以使用 @Synchronized
或 dispatch_once
。即使您不使用多线程,也最好同步共享实例。
+ (instancetype)sharedLocationManager {
static LocationTracker *sharedInstance_ = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance_ = [[LocationTracker alloc] init];
[sharedInstance_ initialize];
});
return sharedInstance_;
}
- (void)initialize {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[_locationManager requestAlwaysAuthorization];
_locationManager.distanceFilter = 10;
if(IS_OS_9_OR_LATER){
_locationManager.allowsBackgroundLocationUpdates = YES;
}
}
您可以在 LocationTracker
class 中实现位置管理器委托。使用 NSNotificationObserver
更新所有 classes,它们都在等待位置更新。
否则按照下面的行。
CLLocationManager *locationManager = [LocationTracker sharedLocationManager].locationManager;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
嗨,我想获取后台位置更新,所以我编写了以下代码并使其同步以防止多线程。所以我有两个问题:
我真的需要同步这段代码吗,我在App里做的只是运行后台任务?我从未创建任何特殊的
NSThread
类型来支持多线程并且不需要这样?每当我需要开始位置更新时,我这样调用:
CLLocationManager *locationManager = [LocationTracker sharedLocationManager]; locationManager.delegate = self; [locationManager startUpdatingLocation];
是正确的调用方式吗?
+ (CLLocationManager *)sharedLocationManager { static CLLocationManager *_locationManager; @synchronized(self) { if (_locationManager == nil) { _locationManager = [[CLLocationManager alloc] init]; _locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; [_locationManager requestAlwaysAuthorization]; _locationManager.distanceFilter = 10; if(IS_OS_9_OR_LATER){ _locationManager.allowsBackgroundLocationUpdates = YES; } } } return _locationManager; }
应该同步共享实例。您可以使用 @Synchronized
或 dispatch_once
。即使您不使用多线程,也最好同步共享实例。
+ (instancetype)sharedLocationManager {
static LocationTracker *sharedInstance_ = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance_ = [[LocationTracker alloc] init];
[sharedInstance_ initialize];
});
return sharedInstance_;
}
- (void)initialize {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[_locationManager requestAlwaysAuthorization];
_locationManager.distanceFilter = 10;
if(IS_OS_9_OR_LATER){
_locationManager.allowsBackgroundLocationUpdates = YES;
}
}
您可以在 LocationTracker
class 中实现位置管理器委托。使用 NSNotificationObserver
更新所有 classes,它们都在等待位置更新。
否则按照下面的行。
CLLocationManager *locationManager = [LocationTracker sharedLocationManager].locationManager;
locationManager.delegate = self;
[locationManager startUpdatingLocation];