ブロックと弱参照に問題があります。ARCの下にいます。私はクラスを作成しました。これは無料のプロジェクトで、Google Directions APIの一種の簡単なラッパーです。ここからダウンロードできます。プロジェクトへのリンク
ビューコントローラー内で使用しています。問題は、使用後のビューコントローラーが割り当てが解除されていません。コメントアウトするか、nilに設定すると、すべてが正しく機能するため、これはこのオブジェクトの問題だと思います。保持サイクルがどこにあるのか理解できません。もちろん、弱い自己に設定しました。これを使用するViewControllerのメソッドは次のとおりです。
- (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];
}
});
}];}
ブレークポイントを設定してViewControllerのretainCountを要求すると、渡されたView Controllerがweakに設定されている場合でも、異なる時間に増分されることがわかります。どんな助けでも本当にありがたいです。
ありがとう、
アンドレア
/ * ** * ** * ** * ** UPDATE * ** * ** * ** * * / **
割り当てを確認するブロック内で、ビューコントローラが何度も保持されていることがわかります。 tillと呼ばれるメソッド-tryRetain
はデクリメントされますが、割り当て解除のために1つのリリースを見逃しているようです。念のため、渡されたブロックがクラスルート方向オブジェクトにコピーされることを指定する必要があります。ここからダウンロードできる小さなサンプルを作成しました:プロジェクトのダウンロード