Persistence scheme & state data for low memory situations (iphone)

Posted by Robin Jamieson on Stack Overflow See other posts from Stack Overflow or by Robin Jamieson
Published on 2009-09-03T17:37:18Z Indexed on 2010/06/07 9:02 UTC
Read the original article Hit count: 213

What happens to state information held by a class's variable after coming back from a low memory situation?

I know that views will get unloaded and then reloaded later but what about some ancillary classes & data held in them that's used by the controller that launched the view?

Sample scenario in question:

@interface MyCustomController: UIViewController
{
    ServiceAuthenticator *authenticator;
}

-(id)initWithAuthenticator:(ServiceAuthenticator *)auth;

// the user may press a button that will cause the authenticator
// to post some data to the service.
-(IBAction)doStuffButtonPressed:(id)sender;
@end

@interface ServiceAuthenticator
{
   BOOL hasValidCredentials; // YES if user's credentials have been validated
   NSString *username;
   NSString *password; // password is not stored in plain text
}

-(id)initWithUserCredentials:(NSString *)username password:(NSString *)aPassword;

-(void)postData:(NSString *)data; 
@end

The app delegate creates the ServiceAuthenticator class with some user data (read from plist file) and the class logs the user with the remote service.

inside MyAppDelegate's applicationDidFinishLaunching:

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{

   ServiceAuthenticator *auth = [[ServiceAuthenticator alloc] initWithUserCredentials:username password:userPassword];

   MyCustomController *controller = [[MyCustomController alloc] initWithNibName:...];
   controller.authenticator = auth;

   // Configure and show the window
   [window addSubview:..];  

   // make everything visible
   [window makeKeyAndVisible];

}

Then whenever the user presses a certain button, 'MyCustomController's doStuffButtonPressed' is invoked.

-(IBAction)doStuffButtonPressed:(id)sender
{
   [authenticator postData:someDataFromSender];
}

The authenticator in-turn checks to if the user is logged in (BOOL variable indicates login state) and if so, exchanges data with the remote service. The ServiceAuthenticator is the kind of class that validates the user's credentials only once and all subsequent calls to the object will be to postData.

Once a low memory scenario occurs and the associated nib & MyCustomController will get unloaded -- when it's reloaded, what's the process for resetting up the 'ServiceAuthenticator' class & its former state?

I'm periodically persisting all of the data in my actual model classes. Should I consider also persisting the state data in these utility style classes? Is that the pattern to follow?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c