我遇到了块和弱引用的问题,我在 ARC 下。我建立了一个类,它是一个免费项目,它是一种围绕 Google Directions API 的简单包装器,您可以在此处下载它:链接到该项目
我在视图控制器中使用它问题是使用它后视图控制器是未解除分配。我想这是这个对象的一个问题,因为如果我将它注释掉或设置为 nil 一切正常。我无法理解保留周期在哪里,当然我设置为弱自我,这是我使用它的视图控制器的方法:
- (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 * ** * ** * ** * * /**
检查分配我可以看到在块内视图控制器被保留了很多次通过 trought调用-tryRetain
until 的方法被递减,但它似乎错过了一个释放释放。为了清楚起见,我必须指定传递的块被复制到类路由方向对象中。我做了一个小样本,你可以在这里下载:下载项目