Question

I have an app where there are five screens.

On each screen, in viewDidLoad I am accessing data from server.

On each screen I have next button.

When I go from screen one to screen five (by clicking Next 4 times), in NSLog, I still see the process done by all previous four view controller.

Is there any way, how can I kill those threads?

In short, I don't want to do any process when I go away from that view i.e. if I go from view 3 to 4, I want to stop the task that I was for view 3.

Getting data of earlier views & waiting for that data (which is unwanted) is not good for app, hence I want like what I explained above.


Edit 1

Below is the code I use for reading the data.

.h

@property(nonatomic, retain) NSMutableData *webData;
@property(nonatomic, retain) NSMutableData *data;

Using below I request the data

.m

NSString *myTMainURL = [NSString stringWithFormat:@"http://www.google.com"];
NSURL *url = [NSURL URLWithString:myTMainURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

For reading, below is how I read.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"didReceiveResponse");
    data = [[NSMutableData alloc] init ];
    [webData setLength: 0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {
    NSLog(@"didReceiveData");
    [data appendData:theData];
    [webData appendData:data];
    NSLog(@"didreceveidata leng===%d===%d", [webData length], [data length]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"connectionDidFinishLoading");

    NSString *myDataFromLink = [[NSString alloc] initWithBytes: [data mutableBytes] length:[data length] encoding:NSUTF8StringEncoding];

    NSLog(@"myDataFromLink====%@--", myDataFromLink);

}
Was it helpful?

Solution

In viewWillDisappear:, send cancel to whatever operation is running.

This, of course, assumes you have a cancelable task/method/operation.

For example, for network requests, if you use NSURLConnection this is the case when you employ the delegate approach. With NSURLConnection's convenient class method sendAsynchronousRequest:queue:completionHandler: this is not possible. Thus, any serious application would use the delegate approach, since a long running asynchronous operation must be cancelable.

OTHER TIPS

You can use NSOperation and cancel the operation when you go to the next view may be in the action of the next button or just in viewWillDisappear: method

Edit

Since You are using NSURLConnection then you can call cancel on the connection in viewWillDisappear:

.h

@property(nonatomic, retain) NSMutableData *webData;
@property(nonatomic, retain) NSMutableData *data;
@property(nonatomic, retain) NSURLConnection *connection;

.m

NSString *myTMainURL = [NSString stringWithFormat:@"http://www.google.com"];
NSURL *url = [NSURL URLWithString:myTMainURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

in viewWillDisappear:

[self.connection cancel]

Since you are using NSURLConnection, You can cancel that connection and stop the NSURLRequest which you are using with your NSURLConnection with following code:

-(void)viewDidDisappear
{
[super viewDidDisappear];
[connection cancel];   // Here connection is your NSURLConnection object.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top