Colin
Colin

Reputation:

NSURLDownload delegate methods on a separate thread

Is anyone aware of a way to receive NSURLDownload's delegate methods on a separate thread, i.e. not the main one? I am using an NSOperationQueue to manage them but at the moment I need to use the performSelectorOnMainThread method to get it too work. The problem with this is that it drives the kernel task crazy reaching about 30% of CPU cycles. Curiously this has only happened since upgrading to SL, when NSOperationQueue changed behaviour (not that I am dissing it, GCD rocks!)

Thanks Colin

Upvotes: 1

Views: 1374

Answers (2)

Wil Shipley
Wil Shipley

Reputation: 9543

My first question is, what are you using NSURLDownload to do? Are you just downloading a bunch of files to the disk, or do you really want the data in memory?

  • If you're downloading a bunch of files to the disk and you don't want to do any special processing, I'd first try just firing off all the NSURLDownloads on the main thread, without bothering with an NSOperationQueue... I mean, how many operations are we talking about? Can they all run concurrently? The callbacks on the main thread shouldn't be too much of a problem, unless you are doing something heavyweight when you get notified you got some data, in which case it seems like...

  • Otherwise, I'd switch to using NSURLConnection. It's specifically documented to call you back on the thread you set it up on, and is more flexible. Of course, it's not as high-level, so if you really want files saved to disk, you're going to have to write the I/O yourself. Shouldn't be a huge hardship - it's like four extra lines of code.

-W

Upvotes: 2

Matthieu Cormier
Matthieu Cormier

Reputation: 2185

NSOperationQueue changed behaviour because it was buggy. It's seems really solid now but yeah, it has a different personality. Reference (http://www.mikeash.com/?page=pyblog/dont-use-nsoperationqueue.html)

Can you give more info on your problem? Do you only need to notify when the download is finished? Are you doing many downloads at once?

Upvotes: 0

Related Questions