![](/img/trans.png)
[英]How can each iCloud user save a user specific subscription in CloudKit?
[英]Can two CloudKit call deadlock each other? How to fix?
在performQuery
内部,正在调用另一个performQuery
。 两者的recordType
参数相同,但predicate
不同。
第二个performQuery
永不返回,该应用程序仅运行并等待CloudKit响应。
伪代码是这样的:
publicDatabase.performQuery(CKQuery(recordType: recordTypeSrc, predicate: predicate), inZoneWithID: nil, completionHandler: {records, error in
if error == nil {
//.. problem is not name collision or reusing the same parameter, coming codepart is called though other methods
publicDatabase.performQuery(CKQuery(recordType: recordTypeSrc, predicate: predicate2), inZoneWithID: nil, completionHandler: {records, error in
//THIS LINE WILL NEVER GET REACHED
if error == nil {
} else {
println(error.localizedDescription)
}
dispatch_semaphore_signal(sema2)
})
dispatch_semaphore_wait(sema2, DISPATCH_TIME_FOREVER)
//..
} else {
println(error.localizedDescription)
}
dispatch_semaphore_signal(sema)
})
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
看起来是信号量线程问题。 您正在重用sama参数。 最后的dispatch_semaphore_signal必须移到else内部的上方。 然后,可以完全删除第一个dispatch_semaphore_wait。 因此它将是这样的:
publicDatabase.performQuery(CKQuery(recordType: recordTypeSrc, predicate: predicate), inZoneWithID: nil, completionHandler: {records, error in
if error == nil {
//.. problem is not name collision, it is nested though other blocks
publicDatabase.performQuery(CKQuery(recordType: recordTypeSrc, predicate: predicate2), inZoneWithID: nil, completionHandler: {records, error in
//THIS LINE WILL NEVER GET REACHED
if error == nil {
} else {
println(error.localizedDescription)
}
dispatch_semaphore_signal(sema)
})
//..
} else {
println(error.localizedDescription)
dispatch_semaphore_signal(sema)
}
})
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
死锁是这样修复的:内部查询在外部查询之后移动。 两种信号量均保留。 外部完成处理程序将在内部完成处理程序启动之前完成。 从CloudKit返回的记录存储在局部变量中。
var records: [AnyObject]! = []
publicDatabase.performQuery(CKQuery(recordType: recordTypeSrc, predicate: predicate), inZoneWithID: nil, completionHandler: {records2, error in
if error == nil {
records = records2
} else {
println(error.localizedDescription)
}
dispatch_semaphore_signal(sema)
})
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
//HERE I can use records
var records3: [AnyObject]! = []
publicDatabase.performQuery(CKQuery(recordType: recordTypeSrc, predicate: predicate2), inZoneWithID: nil, completionHandler: {records4, error in
if error == nil {
records3 = records4
} else {
println(error.localizedDescription)
}
dispatch_semaphore_signal(sema2)
})
dispatch_semaphore_wait(sema2, DISPATCH_TIME_FOREVER)
//HERE I can use records3 too
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.