开发者

Adding NSOperation to NSOperationQueue that starts asynchronous ASIHTTPRequest

So I'm trying to do all my REST calls that download data on the background thread so that the UI stays responsive.

I have a viewcontroller that contains 开发者_如何学编程a NSOperationQueue. I create an instance of my importer class that is a subclass of NSOperation. Inside the main() method of my importer, I am setting up a ASIHTTPDataRequest. I create the request, then its time to kick off the request.

Problem: I ran into a problem when starting the request by calling "startAsynchronous" on the request. The delegate call backs never get called. Its like the request starts, downloads its data, but never calls the delegate callback methods.

My solution: Everything seems to work fine (i.e. callbacks, etc) when I start the request synchronously. Is this the correct solution?

Why does the synchronous call work, but not the asynchronous? I'm modeling my importer class after Apples "TopSongs" sample.


ASIHTTPRequest itself should not block the UI if you run it from the main thread.

  • ASIHTTPRequest isn't designed to be run from a background thread. [ASIHTTPRequest requestFinished] calls the "finished" callback on the main thread; it'll never be received by the background thread, so the background thread's run loop never runs.
  • ASIHTTPRequest is an NSOperation anyway. This is an implementation detail.
  • ASIHTTPRequest runs its network code in a background thread by default, so you're unlikely to see much benefit by sticking it in another thread.

If it's parsing the data that's taking a long time, stick that in an operation.


When the method main() is end executing, NSOperation seted to finished and released, so you newer receive delegate callbacks because delegate is released.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜