1 var label = UILabel()
2 override func viewDidLoad() {
3 super.viewDidLoad()
4 // Do any additional setup after loading the view,
typically from a nib.
5
6 label.frame = CGRect(x:0, y:0, width:320, height:
7 label.text = “Loading……”
8 label.font = UIFont(name:“Arial”, size:24)
9 label.backgroundColor = UIColor.orange
10 label.numberOfLines = 0
11 label.lineBreakMode =
NSLineBreakMode.byWordWrapping
12 self.view.addSubview(label)
13
14 let apiURL =
URL(string:“http://ip.taobao.com/service/getIpInfo.php?
ip=27.156.152.57”)
15
16 let globalQueue = DispatchQueue.global()
17 globalQueue.async{
18 let result = try?Data(contentsOf:apiURL!)
19 let message = String(data:result!, encoding:
String.Encoding.utf8)
20 DispatchQueue.main.async
21 {
22 self.label.text = message
23 }
24 }
25 }
//
1 override func viewDidLoad() {
2 super.viewDidLoad()
3 // Do any additional setup after loading the view,
typically from a nib.
4 print(“开始任务,并显示Loading动画。”)
5
6 let group = DispatchGroup()
7 let globalQueue = DispatchQueue.global()
8
9 globalQueue.async(group:group, execute: {_ in
10 print(“从远程服务器加载用户头像。”)
11 })
12
13 globalQueue.async(group:group, execute: {_ in
14 print(“根据用户id获得年度所有的交易额纪录。”)
15 })
16
17 globalQueue.async(group:group, execute: {_ in
18 print(“根据用户id获得所有被用户收藏的商品名称。”)
19 })
20
21 group.notify(queue:globalQueue, execute:{_ in
22 print(“完成所有任务,隐藏Loading动画。”)
23 })
24 }