0

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)
            }
        }
    }

enter image description here

Juno
  • 347
  • 2
  • 11
  • Don’t mix GCD with async await and don’t go from async await to completion handlers. You are mixing approaches. Pick one (async await) and code will fall into place. – lorem ipsum Jun 05 '23 at 11:10
  • You are using Async/Await and Completion Handlers at same time which is wrong. Pick one with which you are comfortable. Either use await keyword or remove completion, Do all your completion work outside the block after await line – Ghulam Mustafa Jun 05 '23 at 11:53

0 Answers0