这可能与下面的问题类似:在 iPhone 中搜索设备 IP 地址的优化方法 ,但在我的情况下,我需要按批次执行操作,如下面的代码所示:
var isConditionTrue = false
var numProcessed = 0
let dbQueue = dispatch_queue_create("dbQueue", DISPATCH_QUEUE_SERIAL)
// case 1
for i in 1...10 {
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.value), 0)) {
let eachDBValue = ConnectedDevices.getAllDetails(i)
dispatch_async(dbQueue) {
if !eachDBValue {
numProcessed++
}
}
}
}
// case 2
for i in 11...20 {
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.value), 0)) {
let eachDBValue = ConnectedDevices.getAllDetails(i)
dispatch_async(dbQueue) {
if !eachDBValue {
numProcessed++
}
}
}
}
// case 3
for i in 21...30 {
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.value), 0)) {
let eachDBValue = ConnectedDevices.getAllDetails(i)
dispatch_async(dbQueue) {
if !ieachDBValue {
numProcessed++
}
}
}
}
// case 4
for i in 31...40 {
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.value), 0)) {
let eachDBValue = ConnectedDevices.getAllDetails(i)
dispatch_async(dbQueue) {
if !eachDBValue {
numProcessed++
}
}
}
}
如果在情况1下,对于1到10的结果为false,则应转到情况2。如果任何实例的结果为true,则不执行任何情况2、3、4。
同样地,对于情况2,如果1到10的结果为false,则应转到情况3,否则应停止。所有这些都需要根据条件来完成。
for i in 11...20
而不是for i in 0...9
。对于你代码中的每个for
循环都有同样的问题。 - Luca Angelettii
没有被使用。 - Luca AngelettiConnectedDevices.getAllDetails()
的调用。在进行了n
次调用后,您是否期望得到不同的结果? - Luca AngelettiConnectedDevices.getAllDetails(i)
返回一个布尔值。是这样吗? - Luca Angeletti