首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何观察dealloc过程

如何观察dealloc过程
EN

Stack Overflow用户
提问于 2016-09-07 23:44:57
回答 1查看 82关注 0票数 0

在我的应用程序中,我正在下载位置数据并将其显示在地图上。有一些场景,我不能理解为什么会发生。有时,我的mapView(谷歌地图)类dealloc被调用时,我看不到地图上的标记(我仍然可以看到我当前的位置)。

这就是我的mapView在storyBoard中的setUp方式:

代码语言:javascript
复制
 [ContainerVc] -embedSegue->[MapView]

容器Vc是根Vc。

在我的rootVc类中:

代码语言:javascript
复制
   @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类中(所有属性都是强的、非原子的):

代码语言:javascript
复制
  -(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,在这种情况下,我看不到地图上的标记)。

我无法从堆栈跟踪中获得太多信息。我想知道为什么我的mapVc有时会被释放?如何确保我的mapVc在任何时候都不会被释放?

编辑:

我在dealloc案例中记录了mapVc本身:

代码语言:javascript
复制
 <MapVc: 0x7ff83f2883c0> On didAppear

<MapVc: 0x7ff83f2883c0> on dealloc (why is this happening ?)

<MapVc: 0x7ff84475b6f0> new instance again on viewDidAppear
EN

回答 1

Stack Overflow用户

发布于 2016-09-08 00:09:21

如何确保我的mapVc在任何时候都不会被释放?

如果你需要一个VC在它不在屏幕上(或者至少在导航堆栈中)的时候存在,那么你就破坏了MVC。我不认为这是这里正在发生的事情,但重要的是要记住这一点。网络操作应该发生在您的模型层中,并且您的视图层应该观察模型。视图控制器负责管理当前可见的视图,而不是网络操作。您可能没有正确地组织视图控制器。

也就是说,这可能不是本例中的实际问题。问题是,没有任何东西在保留mapVc。它是根视图控制器中的weak,所以它可能会在段完成后立即被释放(段在运行时会保留它)。如果根视图控制器中嵌入了mapVc,那么几乎可以肯定它应该是strong

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39374261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档