Objective c 位置管理器不工作、信号量错误、未调用代理,为什么?
我相信我已经阅读并遵循了大多数关于如何为用户接收位置更新的教程。我有一个类UserTracker,它导入CoreLocation并实现正确的委托协议。我还实现了正确的方法。问题是,当我使用此方法测试类时:Objective c 位置管理器不工作、信号量错误、未调用代理,为什么?,objective-c,ios,gps,automatic-ref-counting,core-location,Objective C,Ios,Gps,Automatic Ref Counting,Core Location,我相信我已经阅读并遵循了大多数关于如何为用户接收位置更新的教程。我有一个类UserTracker,它导入CoreLocation并实现正确的委托协议。我还实现了正确的方法。问题是,当我使用此方法测试类时: -(bool)test { UserTracker * ut = [[UserTracker alloc] init]; [ut startTracking]; [NSThread sleepForTimeInterval:30]; [ut stopTra
-(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
这些都不是被称为。。。这是线程问题吗?为什么会出现这种奇怪的信号量错误?location manager类在这里进行了测试:
int main(int argc, char *argv[])
{
@autoreleasepool
{
// TODO: remove this class
BPTest * bt = [[BPTest alloc] init];
[bt runTests];
// Above must be removed and the location test inserted directly here...
return UIApplicationMain(argc, argv, nil, NSStringFromClass([BPAppDelegate class]));
}
}
作为一种快速的测试方法。。。只有当我试图将测试完全移出BPTest类,并在调用UIApplicationMain之前直接执行它时,才似乎调用了委托方法…为什么要调用[NSThread sleepForTimeInterval:30]?另外,您是否设置了代理?以便我有时间使用模拟器手动输入模拟坐标。输入坐标后,程序应调用记录坐标的回调函数,当所有操作完成后,将进行距离计算。。。委托是在init-method中设置的。您知道模拟器提供坐标吗?你只需要打开它们。我建议尝试一下,看看它是否解决了您的问题。我是通过单击模拟器中的箭头按钮手动提供坐标,还是通过读取坐标文件来提供坐标,两者有什么区别?不,更好的解决方案是在开发人员站点上遵循示例。把睡衣拿出来。不要停止追踪。查看是否调用了委托方法。然后你可以担心你的坐标。