我试图将下面的代码迁移到Swift 3。
func fetchTemplatesForField(_ fieldId: Int, entryGroupId: Int? = 0, templateFieldResponseDelegate: TemplateFieldResponseDelegate) -> Void {
// Dispatch group will maintain the entry and exit of templates api calls.
self.templateFieldGroup = DispatchGroup()
self.templateFieldGroup.enter()
let fetchFieldGroupEndpoint = "\(serviceUrl!)/Actionables/GetTemplatesForField?fieldID=\(fieldId)"
self.progressDataManager.incrementMaxValue(2)
print("endpoint: \(fetchFieldGroupEndpoint)")
AlamofireAPIManager.sharedManager.request(fetchFieldGroupEndpoint)
.validate()
.responseJSON { (response) -> Void in
switch response.result {
case .success:
DispatchQueue.global().async {
print("fetchTemplatesForField: Success")
if let value = response.result.value {
let responseJson = JSON(value)
self.fieldDataManager.saveTemplateFields(responseJson)
self.progressDataManager.incrementCurrentValue(2)
// self.templateFieldGroup.leave()
===> dispatch_group_leave(self.templateFieldGroup)
return
}
self.templateFieldGroup.leave()
}
case .failure(let error):
print("fetchTemplatesForField: error - \(error)")
self.progressDataManager.incrementCurrentValue(2)
self.templateFieldGroup.leave()
return
}
}
templateFieldGroup.notify(queue: DispatchQueue.main) {
templateFieldResponseDelegate.onTemplateFieldSyncComplete(true)
self.templateFieldGroup = DispatchGroup()
}
}
当我转换为最新语法时,Swift想要将行dispatch_group_leave(self.templateFieldGroup)
更改为self.templateFieldGroup.leave()
。
不幸的是,这在Swift 3中在运行时崩溃。原始代码在Swift 2.2中运行良好。我不确定这是怎么做的,所以我有点失落。任何人都可以对此有所了解吗?
编辑: 我发现真正令人困惑的是DispatchGroup的定义。
open class DispatchGroup : DispatchObject {
}
extension DispatchGroup {
@available(iOS 4.0, *)
public func enter()
@available(iOS 4.0, *)
public func leave()
}
如何确定这些方法实际上应该做什么?