how to implement memoryReceiveWarning

87 Views Asked by At

i am in initial phase of development and i want to make sure i am in right direction with regards to memory management.

i have a view controller named LayoutViewController with xib.I have a custom ui-subclass with its xib named LayoutContainerView which basically contains a scrollview. I am using LayoutContainerView in LayoutViewController xib by IBOutlet.

I have an another UIView subclassed, which contains a view with background image, some labels and a transparent button with same frame as of viw.I am adding this custom controll in LayoutContainerView's scrollview.

my view controller .h looks like this:

#import <UIKit/UIKit.h>

@protocol LayoutVcRemovedProtocol;

extern const char* MyConstantKey;
@interface LayoutViewController : UIViewController

// public properties
@property (nonatomic, copy) NSString *databasePath;


@property (nonatomic, weak) id<LayoutVcRemovedProtocol> layoutVcRemovedProtocolDelegate;

@end

@protocol LayoutVcRemovedProtocol<NSObject>

-(void) layoutVcRemovedProtocolMethod;

@end
=========================


**some of relevant code of **implementation** file looks like this:**

 //private stuffs goes here
const char* MyConstantKey = "MyConstantKey";
@interface LayoutViewController () <UIActionSheetDelegate, UIAlertViewDelegate>
@property(nonatomic, weak) IBOutlet LayoutContainerView *layoutContainerView;
@property(nonatomic, weak) IBOutlet UIButton *backbutton;
@property(nonatomic, weak) IBOutlet UILabel *layoutNameLabel;


@property (nonatomic, strong) UIView *baseView;

@property (nonatomic, strong) NSMutableArray *layoutModelArray;


-(IBAction)backButtonPressed;

@end

@implementation LayoutViewController

//my viewDidLoad and viewWillAppear look like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self adjustLayoutContainerFrameAndSetDataBasePath];
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.layoutNameLabel.text = [Utils getLayoutName];
    [self getLatestData];
}

-(void) getLatestData
{
    [self setUpDataSource];
    [self setUpComponentsOnLayoutScreen];
}

#pragma mark - datasource method
-(void)setUpDataSource
{`
    self.layoutModelArray = (NSMutableArray *)[LAYOUTMODULE getAllLayoutData];
}`



-(void)setUpComponentsOnLayoutScreen
{`
    for (int i = 0; i < self.layoutModelArray.count; i++)
    {
        Layout *layout = [self.layoutModelArray objectAtIndex:i];

        [self drawViewWithLayoutObject:layout];
    }

    [self.layoutContainerView.scrollView adjustContentSize];
}

this is what i am trying to manage memory:

-(void) cleanLayoutModelArray
{

    if (self.layoutModelArray != nil && self.layoutModelArray.count >0)
    {
        [self.layoutModelArray removeAllObjects];
    }

}

-(void) cleanComponents
{

    [self.layoutContainerView.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

}

//user events

-(void) placeOrderForLayout:(Layout *)layout
{
    [DELEGATE showLandscapeLoading];
    //web service COMMUNICATION HERE  here
    OrderModule *oModule = [OrderModule sharedModule];

     NSDictionary *requestDictionary = [NSDictionary dictionaryWithObjectsAndKeys:oModule.currentOrder.mOrderId,@"order_id",oModule.currentOrder.mPosId,@"pos_id",[Utils currentDate],@"book_date", layout.componentId, @"component_id", nil];



    BOOL status = [LAYOUTMODULE placeComponentOrderThroughAPI:requestDictionary];

    if (status == TRUE)
    {
        [self performCleanUp];
        [self getLatestData];
    }
    [DELEGATE stopLandscapeLoading];
}

help me or any suggestion for:

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

what ever i try in memoryWarningDelegate view controller becomes black screen.

1

There are 1 best solutions below

0
On

You've got a lot of @properties marked 'weak' in your code - when you mark things as weak, that means that something else is keeping track of whether it should still exist. Here you're doing it with IBOutlet items, which your controller should be keeping track, so they should be marked 'strong,' not weak. I'd review all your usage of 'weak' in this. Also refer to Apple's excellent doc on memory management. ARC will be handling most of your memory management for you. Usually, the only thing you need to do in didReceiveMemoryWarning, is to set to nil anything that's a large object, say a video or webpage you can reload if the user needs it again. Often, there's not much that you can free up at this time in your typical view. Note also that iOS devices have a fairly substantial memory footprint these days, so you should not worry about small data structures remaining resident in memory as long as they have the potential to be needed again. If you're having out of memory issues, I'd run with instruments and check for leaks.