我相信我已经阅读并遵循了大多数关于如何为用户接收位置更新的教程。我有一个类(UserTracker),它导入 CoreLocation 并实现正确的委托协议。我也实施了正确的方法。问题是当我用这种方法测试类时:
-(bool)test
{
UserTracker * ut = [[UserTracker alloc] init];
[ut startTracking];
[NSThread sleepForTimeInterval:30];
[ut stopTracking];
NSLog(@"Accumulated distance is %f m", [pt sumDist]);
return true;
}
我收到以下错误:
libdispatch.dylib`dispatch_semaphore_signal:
0x1e07e02: movl 4(%esp), %eax
0x1e07e06: movl $1, %ecx
0x1e07e0b: lock Thread 1: EXC_BAD_ACCESS (code=2, address=0x20)
0x1e07e0c: xaddl %ecx, 32(%eax)
0x1e07e10: incl %ecx
0x1e07e11: testl %ecx, %ecx
0x1e07e13: jg 0x1e07e22 ; dispatch_semaphore_signal + 32
0x1e07e15: cmpl $2147483648, %ecx
0x1e07e1b: je 0x1e07e25 ; dispatch_semaphore_signal + 35
0x1e07e1d: jmp 0x1e081c1 ; _dispatch_semaphore_signal_slow
0x1e07e22: xorl %eax, %eax
0x1e07e24: ret
0x1e07e25: ud2
顺便说一句,两者都在同一个线程上运行(这是一个问题吗?)。
UserTrack.m 中的一些方法:
-(void)startTracking
{
if(_isTracking)
return;
// Clear old data
[_locations removeAllObjects];
[_lm startUpdatingLocation];
_isTracking = true;
NSLog(@"is tracking!");
}
// Stop tracking GPS coordinates
-(void)stopTracking
{
if (!_isTracking)
return;
if(_lm != nil)
[_lm stopUpdatingLocation];
_isTracking = false;
NSLog(@"STOPPED TRACKING");
}
-(id)init
{
if(self = [super init])
{
_isTracking = false;
_locations = [[NSMutableArray alloc] init];
_lm = [[CLLocationManager alloc] init];
_lm.delegate = self;
_lm.distanceFilter = kCLDistanceFilterNone; // whenever we move
_lm.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
}
return self;
}
也在同一个类中实现:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
// Callback: new GPS coordinate available
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
没有被调用...这是线程问题吗?为什么我会收到这个奇怪的信号量错误?