Luuk D. Jansen
Luuk D. Jansen

Reputation: 4496

Knowing when a view is going to be destroyed in UIView lifecycle

I have a UIView which is part of a UIPageViewController. I pass a strong object to this view, and am having problem with releasing.

In the past I think I used viewDidUnload to release any strong objects, and then Dealloc is called. Now deal is never called because of the strong objects.

What is the best way to know icm with a UIPageViewController that the object is not needed anymore. I.e. if it is the view beside the page the user is looking at, it might come back into view. So using viewWillDisappeart will not work I expect.

@interface DOTourFloorPlanViewController : UIViewController <UIScrollViewDelegate, DOAsyncImageViewDelegate> {
    IBOutlet DOAsyncImageView* _imageView;
    IBOutlet UIScrollView* _scrollView;

    NSMutableArray* _beaconLabels;

    UIView* _circle;
    UIView* _centerDot;

    DOTour* _tour;
    CGRect _zoomRect;

    int _circleCenterX;
    int _circleCenterY;

    bool _didZoomToLocation;
}

@property (strong, nonatomic) DOTour* tour;

Upvotes: 2

Views: 3267

Answers (1)

Wain
Wain

Reputation: 119031

Views aren't unloaded any more, newer devices don't have quite such tight memory restrictions and there are other optimisations. It seems that when you say view you're actually referring to the view controller, which is a little confusing.

If your view controller (VC) is provided with some data it can retain a reference to it until it's destroyed. The data should not be retaining the VC, or its view. If you have any kind of observation / delegation then that link should be weak to prevent retain cycles.

In this way the data will be released when the VC is no longer required, i.e. when it is removed from its parent or presenter.

Specifically for Core Data and references to NSManagedObjects you can help the system by calling refreshObject:mergeChanges: with a flag of NO to turn the object into a fault and remove its data from memory. You can do this when the view did disappear.

Upvotes: 1

Related Questions