I am developing an iPad application, and now I am struggling to find the best approach to multithreading. Let me illustrate this with a simplified example:
I have a view with 2 subviews, a catalog picker and a gallery with thumbnails of all the images in the selected directory. Since loading and creating these thumbnails can take quite a while, I need multithreading, so interaction and updating the view are not blocked.
This is what I have already tried:
[self performSelectorInBackground: @selector (displayThumbnails :) withObject: currentFolder];
This worked fine, because user interaction was not blocked, but it fails when the user enters another folder while the first folder is still loading. Two threads try to access the same view and variables, which leads to incorrect execution of each of them. When users select another folder, the displayThumbnails
download folder should currently be interrupted. I did not find a way to do this.
Nsthreads
I tried this, but struggled with almost the same problems as with the first method, I did not find a (easy) way to cancel the current method. (Yes, I know about [aThread cancel]
, but did not find a way to "resume" the thread). Maybe I need a subclass of NSThread
and implement my own isRunning methods, etc.? But is there a better way or a third (or even fourth and fifth) option that I miss?
I think this is a fairly simple example, and I think a better solution is possible without a subclass of NSThread
. So what would you do? Your opinion please!
s1m0n source share