Reputation: 367
After downloading json File from S3 bucket, I want to convert the json file string to a custom struct model object. But I am continuously getting error as "Cannot pass function of type '(Bool) async throws -> Void' to parameter expecting synchronous function type". Can anyone please help me what this means and how to resolve this issue. Thanks in advance. Stuck at this point for log. please help.
func downloadFileFromS3(client: String, fileName: String, completion:@escaping (Bool)-> Void) {
var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let s3BucketName: String = Constants.UIFileLocation.s3BucketName + client
let s3DownloadKeyName: String = fileName
let expression = AWSS3TransferUtilityDownloadExpression()
completionHandler = { (task, location, data, error) -> Void in
DispatchQueue.main.async(execute: {
let downloadFileURL = documentsUrl.appendingPathComponent(fileName)
try? data?.write(to: downloadFileURL)
completion(true)
})
}
let transferUtility = AWSS3TransferUtility.default()
transferUtility.downloadData(fromBucket: s3BucketName, key: s3DownloadKeyName, expression: expression, completionHandler: completionHandler).continueWith{
(task) -> AnyObject? in
if let error = task.error{
print("Error: \(error.localizedDescription)")
}
if let _ = task.result{
// Do something with downloadtask
}
return nil
}
}
func downloadAppThemeJson(completionHandler: @escaping(Bool) -> Void){
Task{
do{
try await downloadFileFromS3(client: self.selectedClient, fileName: Constants.ScreensUI.appInfo, completion: { downloaded in
if downloaded{
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let url = documentsUrl.appendingPathComponent(fileName)
let data = try await URLSession.shared.data(from: url).0
do{
let customModel = try JSONDecoder().decode(CustomModel.self, from: data)
}
catch{
print("model conversion error \(error.localizedDescription)")
}
}
else{
}
})
}
catch{
completionHandler(false)
}
}
}
Upvotes: 1
Views: 195