Reputation: 511
I am trying to solve my last problem in my app which is the biggest for me.
I have this downloadView shown in the screenshot where I download a file/document from a web server through NSURLConnection asynchronously.
All of the view components work perfectly (download button, progressBarView, abortButton) and so on.
When the download starts, the progress bar increments and I can perfectly abort the download by setting the connection to nil and setting the data length to zero.
:: My challenging problem is that ::
when the download is processed in the background and I click on the "BACK" button and navigate back to the firstView and then navigate back to this downloadView, I lose the access to the download. I can no longer abort it or monitor it. The progress bar resets to zero. However, I can still see the download is going and running through NSLog for progressBar.progress.
I think this problem has something to do with retaining views and accessing threads and keeping a downladView alive when pressing on the "back" button.
Sorry for writing too much but I am trying to clarify this issue well.
here is a basic code to show how I am downloading the file.
-(IBAction)downloadButton:(id)sender{
urlLink= @"http://www.example.com/text.pdf";
NSURLRequest *request= [NSURLRequest requestWithURL:[NSURL URLWithString:urlLink] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
connectionbook = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
// using the regular delegate connectino methods
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{}
Upvotes: 0
Views: 130
Reputation: 41652
The core problem is that when you hit the Back button, the view controller object gets dealloc'd (or it should), and when you revisit that view, you get another view created from scratch.
So you have a couple of options. Have the view which you go back to keep a strong reference, so your view shown here is never really dealloced. Thus you always push the same object. You will need to keeps some state around to deal with the viewWillAppear etc getting called all the time.
The other solution is to have some other persistent object keep the connection and have some way for your view controller showing progress access the information.
Two other comments. You sadi "I can perfectly abort the download by setting the connection to nil and setting the data length to zero", which is not the right way to do it. When you want to stop a connection, you send it [conn cancel], then conn.delegate - nil, then you can release it.
Upvotes: 2