Swift - 取消SynchronousRequest?

时间:2015-10-15 12:21:05

标签: ios swift http request synchronous

我没有iOS和Swift的经验,但已经获得了一个利用NSURLConnection.sendSynchronousRequest向网络服务发送JSON请求的项目。我知道同步请求是阻塞请求,但无论如何我可以取消所述请求吗?

例如,用户按下按钮并取消请求?

2 个答案:

答案 0 :(得分:0)

您可以将请求包装到NSOpertations中,并将您的操作添加到NSOperationsQueue中。

在NSOperations上,您可以随时致电取消。

有关nsoperations和队列的更多信息: http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues

答案 1 :(得分:-2)

只需使用取消() swift func:

Apple Documentation - NSURLConnection.cancel()

Cancels an asynchronous load of a request.

Declaration
 SWIFT
     func cancel()
 OBJECTIVE-C
     - (void)cancel

Discussion
 After this method is called, the connection makes no further delegate method calls. If you want to reattempt the connection, you should create a new connection object.

Availability
 Available in OS X v10.2 and later.