[英]Location manager not working, semaphore error, delegates not being called, why?
我相信我已閱讀並遵循了有關如何為用戶接收位置更新的大多數教程。 我有一個類(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
哪些沒有被調用...這是線程問題嗎? 為什么我會收到這個奇怪的信號量錯誤?
位置管理器類在此處進行了測試:
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]));
}
}
作為一種快速的“ hacky”測試方法...僅當我嘗試將測試完全移出“ BPTest”類並在調用“ UIApplicationMain”之前直接執行它時,才可以使用委托方法叫...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.