So I’m using performSelectorInBackground:@selector(loadStuff) to load things in the background. It takes some time.
The user may want to re-load the items, while the method above is running in the background. If I performSelectorInBackground:@selector(loadStuff) again while the other method is already running, I get all kinds of errors.
Is there an easy way to handle this situation?
I’d like to stop the already-running-in-the-background method, then start the new one. (Or if there is a better way of accomplishing the end goal, that would be fine as well).
If you want to start over, you can cancel the connection and create a new one. Since you’re going to run this method on a background thread you need to make sure that only one thread can access the related instance variables at a time:
Another way to do it is to use a flag to indicate a busy state. For example, if you wanted to just return if the long-running method were already in process on another thread, you could do this: