Is it good approach to use , [self retain]?

I created DownloadAndParseBook class. It will not autorelesed before it gеt any data or network error.

I used [self release], [self retain]. Is it good approach to use [self release], [self retain]? Is DownloadAndParseBook contain any potential bugs?

@implementation GetBooks

-(void) books
{ 
 for(int i =0; i<10; i++)
 {
   DownloadAndParseBook *downloadAndParseBook =
        [[[DownloadAndParseBook alloc] init]autorelease];
   [downloadAndParseBook startLoadingBook];
 }
}
@end


@implementation DownloadAndParseBook

- (id)initWithAbook:(int)bookID 
{
 if(self = [super init])
 { 
    [self retain];        
 }
 return self;
}

- (void)startLoadingBook
{
 [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
  [self release];    
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
 [self saveResultToDatabase];
 [self release];
}


@end 

Answers


Self retaining is very occasionally an appropriate pattern. It's rare, but sometimes in certain kinds of multi-threaded code its important to make sure that you don't vanish in the middle of processing something. That said, this is not one of those times. I'm having trouble imagining a case where your current approach would be helpful. If someone creates your object and then never calls startLoadingBook, then it leaks. If someone calls startLoadingBook, then your object is retained anyway, because NSURLConnection retains its delegate until it finishes.

That said, I believe much of your problem is coming from the fact that your object model is wrong. Neither GetBooks nor DownloadAndParseBook make sense as classes. What you likely mean is BookManager (something to hold all the books) and BookDownloadController (something to manage the downloading of a single book). The BookManager should keep track of all the current BookDownloadControllers (in an NSSet or NSArray ivar). Each BookDownloadController should keep track of its NSURLConnection (in an ivar). You should not just create connections and have them "hang on themselves" (i.e. self-retain). This feels convenient, but it makes the code very hard to deal with later. You have no way to control how many connections you're making. You have no way to cancel connections. It becomes a mess really quickly.


Need Your Help

Form submit (POST) to php page gives blank page unless I refresh

php html jquery-mobile form-submit

I have a simple login page with 2 textfields and a button. When I press "Submit", the URL changes to the php page but I see absolutely nothing at all. I have no idea how to fix it so I'm hoping som...

Is it possible to read assets in Windows Store app using stdio functions?

c++ windows-8 windows-store-apps

I'm porting an existing app written in standard C++ to Windows 8. I've tried using WRL for reading assets but it is big PITA and doesn't fit in multithreading model I'm using. C stdio functions see...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.