我有一個塊和弱引用的問題,我在ARC下。我建立了一個類,它是一個免費的項目,它是一個簡單的谷歌方向API包裝,你可以在這裏下載它:link to the project
Im'm在視圖控制器內使用它的問題是,使用它後視圖控制器不釋放。我想這是這個對象的問題,因爲如果我將它註釋掉或設置爲零,一切正常。我不能明白哪裏是保留週期,當然我設置爲弱的自我,這裏是我用它的視圖控制器的方法:塊和保留週期不能捕捉它
- (void) getDirections{
__weak RouteMapViewController * weakSelf = self;
self.routeObject = [[RouteDirectionsObject alloc]init];
[self.mapView removeAnnotations:self.mapView.annotations];
[self.mapView removeOverlays:self.mapView.overlays];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[_routeObject createDirectionRequestWithStartPoint:weakSelf.startPoint
andEndPoint:weakSelf.endPoint
withCallBackBlock:^(NSError *error, NSDictionary *routeDistance, NSDictionary *routeDuration, MKPolyline *routePolyline, NSArray *routes, NSArray *steps, CLLocation *startPoint, CLLocation *endPoint, NSArray *directions) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
Annotation * startAnnotation = [[Annotation alloc]initWithCoordinate:startPoint.coordinate title:NSLocalizedString(@"YOUR_POSITION_KEY", @"Your position") annotationType:AnnotationTypeStart];
Annotation * endAnnotation = [[Annotation alloc]initWithCoordinate:endPoint.coordinate title:NSLocalizedString(@"AIRPORT_POSITION_KEY", @"Airport position") annotationType:AnnotationTypeEnd];
NSArray * annotationArray = [NSArray arrayWithObjects:startAnnotation, endAnnotation, nil];
weakSelf.routeSteps = steps;
weakSelf.routeDirections = directions;
weakSelf.duration = routeDuration;
weakSelf.distance = routeDistance;
CLLocationDegrees maxLat = -90.0f;
CLLocationDegrees maxLon = -180.0f;
CLLocationDegrees minLat = 90.0f;
CLLocationDegrees minLon = 180.0f;
for (int i = 0; i < weakSelf.routeSteps.count; i++) {
NSDictionary * stepDictCoordinate = [[weakSelf.routeSteps objectAtIndex: i]objectForKey:@"start_location"];
CLLocationCoordinate2D currentLocationCoordinate = CLLocationCoordinate2DMake([[stepDictCoordinate objectForKey:@"lat"]doubleValue], [[stepDictCoordinate objectForKey:@"lng"]doubleValue]);
if(currentLocationCoordinate.latitude > maxLat) {
maxLat = currentLocationCoordinate.latitude;
}
if(currentLocationCoordinate.latitude < minLat) {
minLat = currentLocationCoordinate.latitude;
}
if(currentLocationCoordinate.longitude > maxLon) {
maxLon = currentLocationCoordinate.longitude;
}
if(currentLocationCoordinate.longitude < minLon) {
minLon = currentLocationCoordinate.longitude;
}
}
MKCoordinateRegion region;
region.center.latitude = (maxLat + minLat)/2;
region.center.longitude = (maxLon + minLon)/2;
region.span.latitudeDelta = maxLat - minLat;
region.span.longitudeDelta = maxLon - minLon;
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Error", @"Error alert view title") message:NSLocalizedString(@"KEY_DIRECTIONS_ERROR", @"Alert error message for directions") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[_routesButton setEnabled:NO];
}
else{
[weakSelf.mapView addAnnotations:annotationArray];
[_routesButton setEnabled:YES];
if(routePolyline){
[weakSelf.mapView addOverlay:routePolyline];
}
else{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Error", @"Error alert view title") message:NSLocalizedString(@"KEY_DIRECTIONS_POLYLINE_ERROR", @"Polyline inconsistant") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
//[weakSelf.mapView setRegion:region animated:YES];
[weakSelf setRegion:region];
}
});
}];}
如果我把斷點和討說法控制器的retainCount我可以看到,即使視圖控制器通過設置爲弱,它也會增加不同的時間。 任何幫助將非常感激。
謝謝,
安德烈
/* ** * ** * ** * **UPDATE* ** * ** * ** * */**
檢查分配我可以看到,在塊內視圖控制器保留了很多次通過一個叫做-tryRetain
的方法,直到遞減,但它似乎錯過了釋放一次釋放。 爲了清晰起見,我必須指定傳遞的塊被複制到類路徑方向對象中。 我做了一個小樣本,你可以在這裏下載:download project
還你不應該使用實例變量(塊)自我類,_routerButton例如。 – NeverBe
OMG ...在這裏它正在評論兩個_routeButton實例,一切都正確處理,我認爲它們只是作爲單個實例複製。請把它寫爲答覆,我檢查它。非常感謝。 – Andrea