Asynchronous NSURLConnection with NSOperation

我怕爱的太早我们不能终老 提交于 2019-11-27 12:29:09

This kind of question was asked a zillion time. Delegates are not getting called because as of iOS 4 operations are started on a secondary thread. The thread probably exits before the delegates are called that's all.

Keep the connection on the main thread and handle the data in a background thread using GCD.

I'v wrote about all this stuff here : http://cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/

EDIT : Updated link.

Apple provides QHTTPOperation, which does just what you want, encapsulates an NSURLConnection within an NSOperation, for a single request. You can find it in Apple's sample code.

QHTTPOperation is actually a subclass of QRunLoopOperation which lets you encapsulate logic which depends on run-loop callbacks in an NSOperation.

The 3rd party ASIHTTPRequest is a similar popular solution, AFAIK it is no longer maintained though.

Look at the sendAsynchronousRequest:queue:completionHandler: method documented here in Apple's Documentation. It allows asynchronous requests with NSURLConnection.

Note this requires iOS5 or higher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!