0

I am developing an app with a view to take pictures.

I faced a very strange behavior lately, here is my code.

@interface ViewControllerPhotos : UIViewController 
@property (strong) NSString* _albumID;
@end

@implementation ViewControllerPhotos
@synthesize _albumID;

- (void)didReceiveMemoryWarning
{
   return;
   // commented or not : give the same issue
   //    [super didReceiveMemoryWarning];
   // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationItem setHidesBackButton:YES];

    self._albumID = [Tools generateUUID];

    NSLog(@"new photoset : local UUID \"%@\"", self._photoSetLocalID);
}

@end

My issue is : if there is a memory warning, the UID stored in _albumID is forgotten and regenerated so my album is broken in two. Why ? Should not the strong keyword be able to keep the current value ?

Or is it because the viewDidload is called again ? If it's the case then how to be sure we load our view for the first time for a proper init ? The methods sounded to be designed for it.

dvkch
  • 1,079
  • 1
  • 12
  • 20
  • Objects are strong by default, so the strong keyword does nothing here. Try writing your albumID to file and retrieving it when you receive a memory warning. – Dustin Jul 03 '12 at 12:44
  • `if(!self._albumID || [self._albumID isEqualToString:@""]) self._albumID=[Tools generateUUID];` in `viewDidLoad` solved it ! – dvkch Jul 03 '12 at 12:54

1 Answers1

2

Well, you could figure this out with the debugger and the manual, but...

viewDidLoad is called when the view loads. In a low memory situation when the viewcontroller is not visible, the view may be unloaded. There's a viewDidUnload method in iOS3+. Then when you press the back button and the view becomes visible again, viewDidLoad would be called again as you suspected.

The way around this is either to store the UUID so when its reloaded it's not re-generated.

Or, you could put the assignment in the init method. That way it's only ever called once.

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
  • indeed `viewDiLoad` is called again but `init`. This means every view should init themselves from the `init` method but in this case some graphical parameters like `frame` are not available. This is a weird way of dealing with memory warnings, yet maybe the best, but it seems there is a lack of documentation on it! – dvkch Jul 03 '12 at 12:53