我无法理解调度队列或任务处理程序在 swift 3 中如何工作。我的具体问题是:我遇到性能问题,想要并行运行多个代码块,并等待退出该函数,直到所有 block 都运行完毕完全的。我尝试简单地在后台异步运行这些 block ,但随后我得到“尝试在 xxx 发生变化时使用它”,因为我枚举了在异步队列上管理的相同数组或 enumerateChildNodes(...) 。我正在尝试管理最小化 SpriteKit 节点以提高 fps。以下是我想要并行运行并等待的代码块的示例:
func determinePlatformNodesToUse() {
// Code Block 1:
for platform in platformArray {
addPlatformNode(platform, leftDistance: leftDistance, rightDistance: rightDistance)
}
// Also part of Code Block 1:
// resort the platform nodes to guarantee position sequence
currentPlatformNodeArray.sort(by: { $0.position.x < $1.position.x })
// Code Block 2:
for character in characterArray {
if character.type == CharacterType.Enemy {
addCharacterNode(character, leftDistance: leftDistance, rightDistance: rightDistance)
}
}
// Code block 3
// put all the enemies into an array for update processing will be added in addCharacterNode
currentMotionEnemyNodeArray.removeAll()
// Also part of Code block 3
foregroundNode.enumerateChildNodes(withName: CharacterType.Enemy.rawValue, using: {
(node, stop) in
if let enemy = node as? CharacterNode {
if enemy.motionType != .Stand { // dont use .Stand as they dont have motion
self.currentMotionEnemyNodeArray.append(enemy)
}
}
})
// Code Block 4
for actionSceneObject in actionSceneObjectArray {
addActionSceneObjectNode(actionSceneObject, leftDistance: leftDistance, rightDistance: rightDistance)
}
// At this point, wait until all blocks 1-4 above have finished
// Run blocks 5 - 8 code in all in parallel
// wait until blocks 5 - 8 have finished and then leave the function
}
最佳答案
您可以使用NSOperation
来实现您想要做的事情。可以通过以下方式完成:
func determinePlatformNodesToUse(completion: (_ success: Bool) -> Void) {
let queue:OperationQueue = OperationQueue()
queue.name = "com.SOQA.SOQA.name"
queue.qualityOfService = QualityOfService.default
queue.maxConcurrentOperationCount = 6
let operation01:BlockOperation = BlockOperation {
// Code Block - 1
}
let operation02:BlockOperation = BlockOperation {
// Code Block - 2
}
let operation03:BlockOperation = BlockOperation {
// Code Block - 3
}
let operation04:BlockOperation = BlockOperation {
// Code Block - 4
}
let operation05:BlockOperation = BlockOperation {
// Code Block - 5
}
let operation06:BlockOperation = BlockOperation {
// Code Block - 6
}
let operation07:BlockOperation = BlockOperation {
// Code Block - 7
}
let operation08:BlockOperation = BlockOperation {
// Code Block - 8
}
operation05.addDependency(operation01)
operation05.addDependency(operation02)
operation05.addDependency(operation03)
operation05.addDependency(operation04)
operation06.addDependency(operation01)
operation06.addDependency(operation02)
operation06.addDependency(operation03)
operation06.addDependency(operation04)
operation07.addDependency(operation01)
operation07.addDependency(operation02)
operation07.addDependency(operation03)
operation07.addDependency(operation04)
operation08.addDependency(operation01)
operation08.addDependency(operation02)
operation08.addDependency(operation03)
operation08.addDependency(operation04)
queue.addOperations([operation01, operation02, operation03, operation04, operation05, operation06, operation07, operation08], waitUntilFinished: true)
}
如果您对使用此示例有任何疑问,请随时发表评论。请随意提出修改建议以使其变得更好:)
关于swift - 在 Swift 3 函数中同时运行代码块,然后等待完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40066043/