过去通过 NSURLConnection.sendSynchronousRequest() 方法能同步请求数据。从iOS9起,苹果建议废除 NSURLConnection,使用 NSURLSession 代替 NSURLConnection。
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_816.html
如果想要 NSURLSession 也能够同步请求,即数据获取后才继续执行下面的代码,使用信号、信号量就可以实现。
样例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
//创建NSURL对象
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
(
"数据加载完毕!"
)
//继续执行其他代码.......
|
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_816.html