一、请求超时
// 设置请求最大并发数
URLSessionConfiguration().httpMaximumConnectionsPerHost = xxxx
二、多任务无顺序
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let group = DispatchGroup()
DispatchQueue.global().async(group: group, execute: {
print("1")
})
DispatchQueue.global().async(group: group, execute: {
print("2")
})
DispatchQueue.global().async(group: group, execute: {
print("3")
})
DispatchQueue.global().async(group: group, execute: {
print("4")
})
DispatchQueue.global().async(group: group, execute: {
print("5")
})
DispatchQueue.global().async(group: group, execute: {
print("6")
})
group.notify(queue: DispatchQueue.main) {
print("end")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let block = BlockOperation {
print("1")
}
block.addExecutionBlock {
print("2")
}
block.addExecutionBlock {
print("3")
}
block.addExecutionBlock {
print("4")
}
block.addExecutionBlock {
print("5")
}
block.addExecutionBlock {
print("6")
}
block.addExecutionBlock {
print("7")
}
// 监听任务完成
block.completionBlock = {
print("end")
}
block.start()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let group = DispatchGroup()
group.enter()
DispatchQueue.global().async {
print("1")
group.leave()
}
group.enter()
DispatchQueue.global().async {
print("2")
group.leave()
}
group.enter()
DispatchQueue.global().async {
print("3")
group.leave()
}
group.enter()
DispatchQueue.global().async {
print("4")
group.leave()
}
group.enter()
DispatchQueue.global().async {
print("5")
group.leave()
}
group.enter()
DispatchQueue.global().async {
print("6")
group.leave()
}
group.notify(queue: DispatchQueue.main) {
print("end")
}
}
三、多任务有顺序
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let block1 = BlockOperation {
print("1")
}
let block2 = BlockOperation {
print("2")
}
let block3 = BlockOperation {
print("3")
}
let block4 = BlockOperation {
print("4")
}
let block5 = BlockOperation {
print("5")
}
block2.addDependency(block1)
block3.addDependency(block2)
block4.addDependency(block3)
block5.addDependency(block4)
block1.start()
block2.start()
block3.start()
block4.start()
block5.start()
}
网友评论