2011-10-02 83 views
3

我在設備上使用CLLocationManager並且委託方法沒有被調用。這裏是我的代碼(我可以看到這個類是沒有得到釋放其一):CLLocationManager委託不叫

#import <Foundation/Foundation.h> 
#import <CoreLocation/CoreLocation.h> 

@interface CurrentLocation : NSObject <CLLocationManagerDelegate> { 
    CLLocationManager *locationManager; 

} 
@property (nonatomic, retain) CLLocationManager *locationManager; 

@end 

#import "CurrentLocation.h" 
#import "SBJson.h" 
@implementation CurrentLocation 
@synthesize locationManager; 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     NSLog(@"Initialized"); 
     locationManager = [[[CLLocationManager alloc] init] autorelease]; 
     locationManager.delegate = self; 
     [locationManager startUpdatingLocation]; 
     BOOL enable = [CLLocationManager locationServicesEnabled]; 
     NSLog(@"%@", enable? @"Enabled" : @"Not Enabled"); 
    } 

    return self; 
} 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation { 
    NSLog(@"A"); 
    NSString *stringURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true", newLocation.coordinate.latitude, newLocation.coordinate.longitude]; 
    NSLog(@"%@", stringURL); 

    NSURL *url = [NSURL URLWithString:stringURL]; 

    NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:url] returningResponse:nil error:nil]; 

    SBJsonParser *parser = [[SBJsonParser alloc] init]; 
    NSDictionary *dict = [parser objectWithData:data]; 
    NSLog(@"%@", dict); 
} 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { 
    NSLog(@"B"); 
} 

- (void)dealloc { 
    NSLog(@"Dealloc called"); 
    [super dealloc]; 
    [locationManager release]; 
} 

@end 

在日誌中,我看到:

2011-10-02 19:49:04.095 DixieMatTracker[1404:707] Initialized 
2011-10-02 19:49:04.109 DixieMatTracker[1404:707] Enabled 

我在做什麼錯? 謝謝

+0

? –

+0

包含標題。 – 0xSina

+0

不太可能有所作爲,但我看到了陌生的東西。 放置self.locationManager(訪問屬性)。如果沒有自我,它會訪問備份變量,而不會保留(因爲您已經自動發佈)。 –

回答

6

您正在使用locationManager ivar,而不是屬性;這意味着CLLocationManager未被保留,並且會在您自動解除分配時解除分配。使用self.locationManager

self.locationManager = [[[CLLocationManager alloc] init] autorelease]; 
+0

發生了什麼不能相信我沒有捕獲... :(。謝謝。 – 0xSina

2

我建議你檢查你的CLLocationManager實例的內存管理。

寫這樣:

#import "CurrentLocation.h" 
#import "SBJson.h" 
@implementation CurrentLocation 
@synthesize locationManager; 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     NSLog(@"Initialized"); 
     locationManager = [[CLLocationManager alloc] init]; 
     locationManager.delegate = self; 
     [locationManager startUpdatingLocation]; 
     BOOL enable = [CLLocationManager locationServicesEnabled]; 
     NSLog(@"%@", enable? @"Enabled" : @"Not Enabled"); 
    } 

    return self; 
} 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation { 
    NSLog(@"A"); 
    NSString *stringURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true", newLocation.coordinate.latitude, newLocation.coordinate.longitude]; 
    NSLog(@"%@", stringURL); 

    NSURL *url = [NSURL URLWithString:stringURL]; 

    NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:url] returningResponse:nil error:nil]; 

    SBJsonParser *parser = [[SBJsonParser alloc] init]; 
    NSDictionary *dict = [parser objectWithData:data]; 
    NSLog(@"%@", dict); 
} 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { 
    NSLog(@"B"); 
} 

- (void)dealloc { 
    NSLog(@"Dealloc called"); 
    [super dealloc]; 
    [locationManager release]; 
} 

@end 

委託不會被調用,因爲CLLocationManager對象被釋放之前,它可以提供位置請求的響應。 這是由您的代碼中的autorelease造成的。

除此之外 - 如果使用autorelease,則在dealloc中手動釋放它將是一個錯誤。這可能會導致意外崩潰。

上面的解決方案在init中分配一個CLLocationManager並在dealloc中釋放它。

+0

哇。那工作。仍然不明白爲什麼CLLocationManager實例被釋放...我正在autoreleasing,然後使用原告保留它。無論如何,非常感謝。 – 0xSina

+1

這不能解決潛在的問題:屬性沒有被使用。 –

+0

該物業的使用沒有被問到。順便說一句,這正是如果該屬性被定義爲保留,並已正確使用self.locationManager – Wizz

0

對我來說,那是因爲我的設備無法確定位置 - 而呆在室內GPS信號不可用和Wi-Fi也被斷開,外景經理只是無法接收任何位置數據和委託從未被調用過。

一旦我連接的Wi-Fi,它的工作(我猜的戶外運動也應該做的伎倆在這種情況下,但有時也不是很方便的方式:))的`locationManager`屬性是如何定義的