在我的应用中,我正在下载位置数据并在地图上显示它们。在某些情况下,我无法理解它们为什么会发生。有时,我的mapView(谷歌地图)课程dealloc
被调用,我无法在地图上看到标记(我仍然可以看到我当前的位置)。
这就是我的mapView在storyBoard中设置的方式:
[ContainerVc] -embedSegue->[MapView]
Container Vc是根Vc。
并在我的rootVc类中:
@property (weak, nonatomic) MapVc *mapVc;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString: @"embeddedMapVc"]) {
self.mapVc = [segue destinationViewController];
self.mapVc.delegate = self;
}
}
并且在我的mapVC类中(所有属性都是强大的,非原子的):
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self setupMapView];
}
- (void)setupMapView{
MyLog(@"Map view just refreshed/setup");
//set map view
self.infoWindow = [[MyMapInfoWindow alloc] initWithFrame:CGRectMake(0, 0, 210, 47)];
self.infoWindow.delegate = self;
CLLocation *center = [[MyLocationMonitor sharedInstance] getLocation];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:center.coordinate.latitude longitude:center.coordinate.longitude zoom:18];
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.mapView.camera=camera;
[self.mapView setMapType:kGMSTypeNormal];
[self.mapView setMinZoom:14 maxZoom:18];
[self.mapView setDelegate:self];
self.mapView.myLocationEnabled=YES;
self.mapView.settings.rotateGestures = NO;
self.mapView.settings.myLocationButton = YES;
[self.mapView setPadding:UIEdgeInsetsMake(0, 0, 62, 0)];
self.view = self.mapView;
[self setupMarkers];
}
- (void)setupMarkers{
MyLog(@"setting up markers");
self.annotations = nil;
[self.mapView clear];
[[MyLocationMonitor sharedInstance] getBuildingsWithCompletion:^(BOOL success, NSArray *buildings) {
self.buildings = buildings;
MyLog(@"_buildings_: %@", self.buildings);
if (success) {
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];
self.annotations = [[NSMutableArray alloc] init];
for (NSDictionary *location in buildings) {
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([[location objectForKey:@"Latitude"] doubleValue], [[location objectForKey:@"Longitude"] doubleValue]);
bounds = [bounds includingCoordinate:coordinate];
GMSMarker *marker = [GMSMarker markerWithPosition:coordinate];
marker.title = [location objectForKey:@"name"];
marker.map = self.mapView;
marker.icon = [UIImage imageNamed:@"Boost-BuildingMarkerIcon"];
marker.userData = @{@"building":location};
marker.infoWindowAnchor = CGPointMake(1.0f, -0.1f);
marker.opacity = 1.0;
[self.annotations addObject:marker];
}
[self.mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:50.0]];
}
}];
}
-(void)dealloc{
MyLog(@"MApVC dealloc called");
}
所以,从rootVc
移动到另一个控制器,在我完成网络调用后,我回到rootVc,prepareForSegue
被触发,调用转到mapVc类,然后设置mapView(在这个过程中,mapVc dealloc有时被调用,在这种情况下,我无法在地图上看到标记)。
我无法从stacktrace获得太多信息。我想知道为什么我的mapVc有时会被解除分配?如何确保我的mapVc不会在任何时候被解除分配?
编辑:
我在dealloc案例中记录了mapVc self:
<MapVc: 0x7ff83f2883c0> On didAppear
<MapVc: 0x7ff83f2883c0> on dealloc (why is this happening ?)
<MapVc: 0x7ff84475b6f0> new instance again on viewDidAppear
答案 0 :(得分:1)
如何确保我的mapVc不会在任何时候解除分配?
如果您在VC不在屏幕上(或至少在导航堆栈中)时需要VC,那么您已经破坏了MVC。我不认为这里发生了什么,但记住这一点非常重要。网络操作应该在您的模型层中进行,您的视图层应该观察模型。视图控制器负责管理当前可见的视图,而不是网络操作。您可能错误地组织了视图控制器。
那就是说,在这种情况下,这可能不是实际问题。问题是没有任何东西保留mapVc
。它是根视图控制器中的weak
,因此一旦segue完成它就可能被释放(segue在运行时保留它)。如果mapVc
嵌入在根视图控制器中,它几乎肯定是strong
。