加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift - 使用NSURLSession同步获取数据(通过添加信号量)

发布时间:2020-12-14 07:02:17 所属栏目:百科 来源:网络整理
导读:首先非常感谢代码的原作者,为了查询方便我就在这里又留了一份,请见谅. 原文http://www.hangge.com/blog/cache/detail_816.html 过去通过 NSURLConnection.sendSynchronousRequest() 方法能同步请求数据。从iOS9起,苹果建议废除 NSURLConnection,使用 NSURL

首先非常感谢代码的原作者,为了查询方便我就在这里又留了一份,请见谅.

原文http://www.hangge.com/blog/cache/detail_816.html

过去通过 NSURLConnection.sendSynchronousRequest() 方法能同步请求数据。从iOS9起,苹果建议废除 NSURLConnection,使用 NSURLSession 代替 NSURLConnection。

如果想要 NSURLSession 也能够同步请求,即数据获取后才继续执行下面的代码,使用信号、信号量就可以实现。

//创建NSURL对象
let urlString:String="http://www.hangge.com"
let url:NSURL! = NSURL(string:urlString)
//创建请求对象
let request:NSURLRequest = NSURLRequest(URL: url)

let session = NSURLSession.sharedSession()

let semaphore = dispatch_semaphore_create(0)

let dataTask = session.dataTaskWithRequest(request,completionHandler: {(data,response,error) -> Void in
        if error != nil{
            print(error?.code)
            print(error?.description)
        }else{
            let str = NSString(data: data!,encoding: NSUTF8StringEncoding)
            print(str)
        }

        dispatch_semaphore_signal(semaphore)
}) as NSURLSessionTask

//使用resume方法启动任务
dataTask.resume()

dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER)
print("数据加载完毕!")
//继续执行其他代码.......

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读