Iโve been studying Objective-C myself for some time now and still donโt quite understand how memory management works. When should properties be released?
For example, I have a class that will handle 2 (register and updateParticulars) different URLRequest connections. updateParticularsConnection will be executed when registration is complete.
@interface ConnectionViewController : UIViewController { } @property (nonatomic, retain) NSURLConnection *registerConnection; @property (nonatomic, retain) NSURLConnection *updateParticularsConnection; @property (nonatomic, retain) NSMutableData *responseData; @property (nonatomic, retain) NSMutableURLRequest *requestURL; @end @implementation ConnectionViewController @synthesize registerConnection, updateParticularsConnection, responseData, requestURL, (void)performRegistration {
Delegate Callback Handling
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [SVProgressHUD dismissWithError:@"Unable to connect"]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { if (responseData == nil) { responseData = [[NSMutableData alloc] init]; } [responseData appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { if (connection == registerConnection) { NSMutableString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; NSLog(@"Register connection recieved data reads : %@", responseString); if ([responseString isEqualToString:@"-1"]) { // error. stop connection [self.requestURL release]; // remember to release requestURL since we would not be continuing on. } else if ([responseString isEqualToString:@""]) { // error. stop connection [self.requestURL release]; //remember to release requestURL since we would not be continuing on. } else { [self updateParticulars]; // perform next connection, updateParticulars } responseData = nil; // clear the current stored data in preparation for the next connection. [self.registerConnection release]; [responseString release]; } // end of definition for register connection else if (connection == updateParticularsConnection) { // do stuff with data received back here self.responseData = nil; [self.requestURL release]; [self.updateParticularsConnection release]; } }
My question is, should I release my properties as soon as I can, what I think I'm doing now? Or just during the dealloc method? Advise if I do not do it right.
source share