The application is made of navigation views.
I am calling a function in viewDidAppear which is
[self extractInfoWithWebsite:[NSString stringWithFormat:@"http://www.cineklik.com/%@-%@.aspx", self.cinemaType, self.location]];
This function does HTML parse from the a current webpage on the internet.
The code is kind of long to share but the idea is that it extract data from a website.
The problem is that I am having is that if the user decides to click the back button of the navigation before that the information is loaded, the page stays the same and the application stops functionning (not a crash but becomes static, doesn’t do anyth anymore).
Is there a solution to this?
Thank you in advance
EDIT:
I changed the code to the following:
NSOperationQueue *queue = [NSOperationQueue new];
/* Create our NSInvocationOperation to call loadDataWithOperation, passing in nil */
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(extractInfoWithWebsite:)
object:[NSString stringWithFormat:@"http://www.cineklik.com/%@-%@.aspx", self.cinemaType, self.location]];
/* Add the operation to the queue */
[queue addOperation:operation];
[operation release];
Now I am able to press the back button without freezing, but now there is a crash with the following error:
Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now…
I think it s because my function was still running in the background.
How to solve this issue?
I would look up NSOperation
The standard approach inside an NSOperation is to check if they have been cancelled before doing heavy processing.
Here is a good link to start your research from.
EDIT: I thought I would clarify the cancelled part
NSOperations have a main() and inside main() it is good practice to do something like