Hi all I have recently migrated my iOS app to Swift 3.1 (Xcode 8.3.3). I have figured out most of the issues on my own but one last bug is still haunting me. Well, to cut the story short, I used Alamofire library for making call to webservices and used Dispatch Sync in few methods as follows:
class func createVideoActivity(_ type: Int, permission: Int, message: String, video: URL, progressview:UIProgressView , completion: @escaping (_ type: ResponseType , _ response : Int, _ message: String) -> Void) {
let user_id = CFunctions.getSession("id")
var serviceURL = baseURL + "&task=createActivity&user_id=\(user_id)&type=\(type)&permission=\(permission)"
serviceURL = serviceURL.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
let url = URL(string: serviceURL)
let authHeader = ["":""]
let mimetype = "video/mov"
var movieData: Data?
do {
movieData = try Data(contentsOf: URL(fileURLWithPath: (video.relativePath)), options: NSData.ReadingOptions.alwaysMapped)
} catch _ {
movieData = nil
return
}
let filename = "upload.mov"
upload(
multipartFormData:{ multipartFormData in
multipartFormData.append(movieData!, withName: "filedata",fileName: filename,mimeType: mimetype)
multipartFormData.append(message.data(using: String.Encoding.utf8)!, withName: "message")
},
to: url!,
headers: authHeader,
encodingCompletion:
{
encodingResult in
switch encodingResult {
case .success(let uploads, _, _):
.uploadProgress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
DispatchQueue.main.async {
let percent = (Float(totalBytesWritten) / Float(totalBytesExpectedToWrite))
print(percent)
progressview.setProgress(percent, animated: true)
}
}
uploads.validate()
uploads.responseJSON { serverResponse in
switch serverResponse.result {
case .success(let JSON):
debugPrint(JSON)
if (JSON as AnyObject).value(forKey: "status") as! Int == 1 {
completion( ResponseType.kresponseTypeSuccess,(JSON as AnyObject).value(forKey: "status") as! Int, (JSON as AnyObject).value(forKey: "response") as? String ?? "")
} else {
completion( ResponseType.kresponseTypeSuccess,(JSON as AnyObject).value(forKey: "status") as! Int, (JSON as AnyObject).value(forKey: "response") as? String ?? "")
}
case .failure(let error):
let dataString = String(data: serverResponse.data!, encoding: String.Encoding.utf8)
print("createVideoActivity Request failed with error: \(String(describing: dataString))")
completion(ResponseType.kResponseTypeFail, error as! Int, "Service failed")
}
}
case .failure(let encodingError):
print(encodingError)
}
}
) // upload - end
}
I am getting "Ambiguous reference to member 'async(execute:)'" error on Dispatch.main.sync line. Can you people find out what's going on?
The issue was not with DispatchQueue.main.async
but with Alamofire's uploadProgress syntax, replaced it with the following block and issue got resolved:
uploads.uploadProgress { (progress: Progress) in
DispatchQueue.main.async {
progressview.setProgress(progress.fractionCompleted, animated: true)
}
}