I have the following sequence:
- Start MBProgressHUD spinner
- Download several images using NSConnectionURL and implemented connectionDidFinishLoading delegate to show the image
- Stop spinner after all images are downloaded and show the parent view that contains all the images
How should I implement the connectionDidFinishLoading: to determine whether all the images are downloaded so that I can stop the spinner?
Should I be tracking the number of downloads, but what about thread safety?
Or should I be looking at GCD?
I would use an
NSOperationQueueand submit the downloads as individual block operations with synchronousNSURLConnectionrequests. You can set the maximum number of concurrent operations there, so that you don’t download everything at once, and you get cancelling for free. Tracking the finish can be done simply by submitting another operation to the queue – the last operation submitted will run after all downloads have finished. This would probably require limiting the number of concurrent operations to one, though, so an alternative is to watch (KVO) the number of remaining operations. When that drops to zero, you’re done.