使用NSURLSession进行同步请求

时间:2015-11-17 12:04:55

标签: ios multithreading swift nsurlsession

我在另一个线程中调用了一些函数:

<div id="world">
  <map id="world">
    <area shape="circle" alt="Africa" title="Africa" coords="682,462,97" href="AllItems.aspx" target="_self" onmouseover="afr.style.visibility='visible'; afr.src='http://irvingglobalstudies.wikispaces.com/file/view/Africa.png/174406561/284x300/Africa.png';"
    onmouseout="afr.style.visibility='hidden';" />
    <area shape="circle" alt="Americas" title="Americas" coords="228,198,123" href="AllItems.aspx" target="_self" onmouseover="ame.style.visibility='visible'; ame.src='https://www.walmartethics.com/images/landingpagemap/americas.png';" onmouseout="ame.style.visibility='hidden';"
    />
    <area shape="circle" alt="Asia/Pacific" title="Asia/Pacific" coords="1110,412,122" href="AllItems.aspx" target="_self" onmouseover="asi.style.visibility='visible'; asi.src='http://www.awlgrip.com/PublishingImages/AsiaPacific.png';" onmouseout="asi.style.visibility='hidden';"
    />
    <area shape="circle" alt="Europe" title="Europe" coords="627,136,98" href="AllItems.aspx" target="_self" onmouseover="eur.style.visibility='visible'; eur.src='http://scrubs.co.uk/_img/map-europe.png';" onmouseout="eur.style.visibility='hidden';" />
  </map>
  <img alt="Africa" style="position:absolute;top:559px;left:779px;visibility:hidden;" id="afr" src="http://irvingglobalstudies.wikispaces.com/file/view/Africa.png/174406561/284x300/Africa.png" />
  <img alt="Americas" style="position:absolute;top:321px;left:351px;visibility:hidden;" id="ame" src="https://www.walmartethics.com/images/landingpagemap/americas.png" />
  <img alt="Asia/Pacific" style="position:absolute;top:534px;left:1232px;visibility:hidden;" id="asi" src="http://www.awlgrip.com/PublishingImages/AsiaPacific.png" />
  <img alt="Europe" style="position:absolute;top:234px;left:725px;visibility:hidden;" id="eur" src="http://scrubs.co.uk/_img/map-europe.png" />
  <img alt="world map" src="https://upload.wikimedia.org/wikipedia/commons/f/fc/No_colonies_blank_world_map.png" usemap="#world" />
</div>
<img alt="Ta Da" src="http://i3.kym-cdn.com/entries/icons/original/000/008/119/1353379_700b.png" />

然后在这个函数中我想做同步请求:

dispatch_async(dispatch_get_global_queue(priority, 0)) {
     self.someFunction()
}

但是线程在此行停止let config = NSURLSessionConfiguration.defaultSessionConfiguration() let session = NSURLSession(configuration: config) let dataSemaphore = dispatch_semaphore_create(0) let task = session.dataTaskWithRequest(urlRequest, completionHandler: { (data, response, error) in guard let responseData = data else { print("Error: did not receive data") return } guard error == nil else { print("error calling GET") print(error) return } print("ASYNC") }) task.resume() dispatch_semaphore_wait(dataSemaphore, DISPATCH_TIME_FOREVER) print("SYNC")

2 个答案:

答案 0 :(得分:1)

你从未发信号通知信号量,把它放在完成处理程序的末尾:

dispatch_semaphore_signal(dataSemaphore)

答案 1 :(得分:0)

虽然这里的其他答案在技术上是正确的,但我觉得有必要发布第二个答案,问为什么你这样做....

同步网络几乎从未采取任何行动的正确方式。相反,你真的应该让函数的其余部分成为一个在请求完成时运行的块。

从技术上讲,为异步API创建同步包装器这一事实并没有否定这样做的事实,即这样做会阻止运行此代码的任何线程,所以除非您处于需要强制执行某些操作的情况对网络请求进行严格排序,同步请求通常是个坏主意,即使这样,它们也不是的想法。