Swift-强制同步执行工作和后续UI呈现
我有一段遵循工作流的代码:Swift-强制同步执行工作和后续UI呈现,swift,multithreading,synchronous,Swift,Multithreading,Synchronous,我有一段遵循工作流的代码: for index in 0..<self!.recordedData.count { //Do work here that involves fetching data from coredata and prerendering an image for each index } reloadUIWithData() 通过以下方式: DispatchQueue.background(delay: 0.01, background: { for index
for index in 0..<self!.recordedData.count {
//Do work here that involves fetching data from coredata and prerendering an image for each index
}
reloadUIWithData()
通过以下方式:
DispatchQueue.background(delay: 0.01, background: {
for index in 0..<self!.recordedData.count {
//Do work here that involves fetching data from coredata and rendering an image for each index
}
}, completion:{
reloadUIWithData()
}
DispatchQueue.background(延迟:0.01,背景:{
对于0..中的索引,有一个专用的APIDispatchGroup
来执行此操作。但是它不会使异步任务同步。它就像一个计数器,在enter
上递增,在leave
上递减,并在notify
上通知循环中的所有任务完成时(计数器==0)
let group=DispatchGroup()
对于0..中的索引,有一个专用的APIDispatchGroup
来执行此操作。但是它不会使异步任务同步。它就像一个计数器,在enter
上递增,在leave
上递减,并在notify
上通知循环中的所有任务完成时(计数器==0)
let group=DispatchGroup()
对于0中的索引。。
DispatchQueue.background(delay: 0.01, background: {
for index in 0..<self!.recordedData.count {
//Do work here that involves fetching data from coredata and rendering an image for each index
}
}, completion:{
reloadUIWithData()
}
let group = DispatchGroup()
for index in 0..<self!.recordedData.count {
group.enter()
doAsynchronousStuff { result in
// do something with result
group.leave()
}
}
group.notify(queue: DispatchQueue.main) {
self.reloadUIWithData()
}