我无法理解调度队列或任务处理程序如何在swift 3中工作。我的具体问题是:我遇到性能问题,并希望并行运行多个代码块并等待退出函数直到所有块完成。我试图在后台异步地运行这些块,但后来我试图在它变异时使用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
}
答案 0 :(得分:0)
您可以使用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)
}
如果您对使用此示例有任何疑问,请随时发表评论。随意建议编辑以使其更好:)