dispatch_async 对比 dispatch_sync 获取数据。 Swift
dispatch_async vs dispatch_sync in fetch data. Swift
看了那么多关于并行和并发的帖子,我还是搞不清楚什么是正确的取数据方式。例如,在我的项目中,我有一个供用户获取数据的按钮。我的代码如下所示。
var array = [Int]()
func fetchData() {
....
....
response(objects: [object], error: NSError?) {
for object in objects {
array.append(object.number) // assume object.number return an Int
}
// confuse here. Should I use async here because I am worry if the user
// click the fetchData button more than one time, the append and make
// function will be happened at the same time. Or, is there anything I
// made a wrong assumption? I guess I need a serial operation. Correct?
dispatch_async(dispatch_get_main_queue()) {
makeCollectionView() // using the data in array
}
}
}
更新
已尝试 运行 此代码。 10000-19999 是 运行 在 0-9999 之后。似乎第二个异步不会停止第一个异步来处理它的操作。
dispatch_async(dispatch_get_main_queue(), { () -> Void in
for i in 0 ..< 10000 {
print(i)
}
})
dispatch_async(dispatch_get_main_queue(), { () -> Void in
for i in 10000 ..< 20000 {
print(i)
}
})
为了提高性能,任何涉及 UI 的东西都应该在主线程上 运行。所以基本上:
dispatch_async(dispatch_get_main_queue()) {
//anything that involves UI
}
GCD 提供队列来执行任务。队列可以有两种类型——concurrent 或 serial。在串行队列中,一次执行一个任务(按 FIFO 顺序),在并发队列中,一次执行多个任务。
为防止用户在一个取任务运行时取数据,此时需要不将取任务提交到队列中。无论是哪种队列——并发或串行。
var array = [Int]()
var isFethingData = false
func fetchData() {
if !isFethingData {
isFethingData = true
dispatch_async(queueForFetchData) {
…
response(objects: [object], error: NSError?) {
for object in objects {
array.append(object.number)
}
dispatch_async(dispatch_get_main_queue()) {
makeCollectionView()
}
isFethingData = false
}
}
}
dispatch_async
和 dispatch_sync
是将任务提交到队列的函数。区别在于 dispatch_async
return 任务提交后立即执行,但 dispatch_sync
等待任务完成。例如:
print("\(NSDate()) qq")
dispatch_sync(queue) {
// … some code which runs for 10 minutes.
print("\(NSDate()) ee")
}
print("\(NSDate()) ww")
// 2016-08-18 16:02:00 qq
// 2016-08-18 16:12:00 ee
// 2016-08-18 16:12:00 ww
print("\(NSDate()) qq")
dispatch_async(queue) {
// … some code which runs for 10 minutes.
print("\(NSDate()) ee")
}
print("\(NSDate()) ww")
// 2016-08-18 16:02:00 qq
// 2016-08-18 16:02:00 ww
// 2016-08-18 16:12:00 ee
看了那么多关于并行和并发的帖子,我还是搞不清楚什么是正确的取数据方式。例如,在我的项目中,我有一个供用户获取数据的按钮。我的代码如下所示。
var array = [Int]()
func fetchData() {
....
....
response(objects: [object], error: NSError?) {
for object in objects {
array.append(object.number) // assume object.number return an Int
}
// confuse here. Should I use async here because I am worry if the user
// click the fetchData button more than one time, the append and make
// function will be happened at the same time. Or, is there anything I
// made a wrong assumption? I guess I need a serial operation. Correct?
dispatch_async(dispatch_get_main_queue()) {
makeCollectionView() // using the data in array
}
}
}
更新
已尝试 运行 此代码。 10000-19999 是 运行 在 0-9999 之后。似乎第二个异步不会停止第一个异步来处理它的操作。
dispatch_async(dispatch_get_main_queue(), { () -> Void in
for i in 0 ..< 10000 {
print(i)
}
})
dispatch_async(dispatch_get_main_queue(), { () -> Void in
for i in 10000 ..< 20000 {
print(i)
}
})
为了提高性能,任何涉及 UI 的东西都应该在主线程上 运行。所以基本上:
dispatch_async(dispatch_get_main_queue()) {
//anything that involves UI
}
GCD 提供队列来执行任务。队列可以有两种类型——concurrent 或 serial。在串行队列中,一次执行一个任务(按 FIFO 顺序),在并发队列中,一次执行多个任务。
为防止用户在一个取任务运行时取数据,此时需要不将取任务提交到队列中。无论是哪种队列——并发或串行。
var array = [Int]()
var isFethingData = false
func fetchData() {
if !isFethingData {
isFethingData = true
dispatch_async(queueForFetchData) {
…
response(objects: [object], error: NSError?) {
for object in objects {
array.append(object.number)
}
dispatch_async(dispatch_get_main_queue()) {
makeCollectionView()
}
isFethingData = false
}
}
}
dispatch_async
和 dispatch_sync
是将任务提交到队列的函数。区别在于 dispatch_async
return 任务提交后立即执行,但 dispatch_sync
等待任务完成。例如:
print("\(NSDate()) qq")
dispatch_sync(queue) {
// … some code which runs for 10 minutes.
print("\(NSDate()) ee")
}
print("\(NSDate()) ww")
// 2016-08-18 16:02:00 qq
// 2016-08-18 16:12:00 ee
// 2016-08-18 16:12:00 ww
print("\(NSDate()) qq")
dispatch_async(queue) {
// … some code which runs for 10 minutes.
print("\(NSDate()) ee")
}
print("\(NSDate()) ww")
// 2016-08-18 16:02:00 qq
// 2016-08-18 16:02:00 ww
// 2016-08-18 16:12:00 ee