views:

480

answers:

2

Hey,

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

A: 

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?

Matthieu Cormier
I've done some more research and my application which relied on the previous implementation of NSOperationQueue has now changed so that it should support both ( hopefully ). But that doesn't change the CPU overload. It still clocks up about 30%. This does not change whether I have 1 download or 30... And yes, I need to be notified continuously of updates. This cpu usage disappears when I do not specify a delegate for the download.
The usage is high even if the delegate does nothing?
Matthieu Cormier
Yes, it doesn't appear to make a difference whether there is a lot of processing going on in the callback routines or nothing at all.
+2  A: 

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

Wil Shipley