Reputation: 763
I have a tableviewapplication, wich when the user select one view it needs to parse some XML to display information.But sometimes the XML is not finished downloading and the user can press the button to select the other view,generating a crash.I think i need to cancel the connection or something to dont cause any conflitct with the new connection,but i dont know exactly how,it suppose to be in ViewWillDisappear correct? Heres how i start the connection on ViewDidAppear:
NSMutableURLRequest * req = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://Adress"]
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f];
conn = [NSURLConnection connectionWithRequest:req delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if(conn)
{
receivedData = [[NSMutableData alloc]init];
[DSBezelActivityView newActivityViewForView:self.view withLabel:@"Loading..."];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if(conn) [conn cancel];
}
Upvotes: 1
Views: 5507
Reputation: 3221
Absolutely you can cancel NSURLConnection
.
But you should be careful in cancelling it too.
If you decide to cancel
it in viewWillDisappear
,
then
You should not do,
autorelease
and also you should not,
release
it anywhere.
Here below the brief explaination:
Do cancel
your NSURLConnection
delegate in viewWillDisappear
[nsurlconnection cancel];
and also you should release
it here not anywhere,
[nsurlconnection release];
If you release the connection
in somewhere like after your xml response
received then,
It will call the viewWillDisappear
method anyway,
here you are cancelling it , then it will lead your app to crash.
"deallocated objects will not be cancelled"
.
And also another situation will occurs while cancelling,
If user comes and immediately navigates other view first time, your nsurlconnection
will be cancelled in viewWillDisappear
method.
Again the user comes to the view
and escapes immediately before your nsurlconnection
initialized or allocated, also your app will be crashed because,
"deallocated objects will not be cancelled".
So, check your connection != nil
before you cancel
it
and also don't forgot to do
nsurlconnection = nil;
in the same time.
So that you can avoid the immediate calls [nsurlconnection cancel] crashes.
SO Finally , in your viewWillDisappear
method you have to do is,
- Need to check nsurlconnection != nil
- Need to cancel it
- Need to allocate null to your nsurlconnection
- Need to release it in the same method.
Sample code will be like the following,
- (void) viewWillDisappear:(BOOL)animated
{
if (nsurlconnection != nil)
{
[nsurlconnection cancel];
[nsurlconnection release];
nsurlconnection = nil;
}
}
Hope it's helpful... Happy coding ...
Upvotes: 2
Reputation: 8292
You can call NSURLConnection's cancel method and it will prevent your connections delegate from being called with any more data. You could do this in viewWillDisappear if that's when it makes sense given how your app works.
Upvotes: 3