0

I have a function that returns an array of objects and I am using alamofire fort he network calls. My functions works absolutely fine and I can see from the console that I am populating my array. My problem is returning this array. If place the return value before the '//END OF COMPLETION HANDLER' I get the error :

 Unexpected non-void return value in void function

If I place the return value after the completion handler I always get returned an empty array even though the console clearly show that my array is being populated. Please can someone advise?

 func returnFolderContents(foldername: String) -> [ScanObj] {

    // GET API KEY
    guard let API_KEY = UserDefaults.standard.string(forKey: "api_key") else{return []}
    var arrayScanObj: [ScanObj] = []

    let param: [String:String] = ["getscans":"forfolder", "folderName": foldername, "api_key": API_KEY]
    Alamofire.request(GET_FOLDER_URL, method: .post, parameters: param).responseJSON { response in

        if let json = response.result.value{

            // CASTING JSON
            let jsonObj = json as! [String:Any]
            let code = jsonObj["code"] as! Int
            print("code: \(code)")

            if(code != 104){
                // NO RECORDS FOUND
                self.showPopUp(msg: "Sign In Fail")
                return
            }

            DispatchQueue.main.async{
                let content = jsonObj["arrayScanObj"] as! [[String: Any]]
                for elem in content{

                    let scanId = elem["scanId"] as! String
                    let pdfPath = elem["pdfPath"] as! String
                    let thumbWebPath = elem["thumbWebPath"] as! String
                    let arrayHashTags = elem["arrayHashTags"] as! [String]

                    // URL = S3_DOMAIN + '/' + BUCKETNAME + '/' + path;
                    let thumbNailURL = self.S3_DOMAIN + "/" + self.BUCKETNAME + "/" + thumbWebPath
                    let pdfURL = self.S3_DOMAIN + "/" + self.BUCKETNAME + "/" + pdfPath

                    // CONSTRUCT HASHTAG STRING FOR DISPLAY
                    var stringHashTags = ""
                    for tag in arrayHashTags{
                        stringHashTags = stringHashTags + "#" + tag + " "
                    }

                    // CREATE ScanObj AND
                    let scanObj = ScanObj.init(thumbNailURL: thumbNailURL, PdfURL: pdfURL, hashTags: stringHashTags, scanId: scanId)
                    arrayScanObj.append(scanObj)
                    print("arrayScanObj.count PRE: \(arrayScanObj.count)")

                }// END FOR-LOOP
                 // return arrayScanObj - I have tried this also [error: 'Unexpected non-void return value in void function']
            }
        }else{
           // return arrayScanObj - I have tried this also [error: 'Unexpected non-void return value in void function']
        }
    }// END OF COMPLETION HANDLER

    print("arrayScanObj.count: \(arrayScanObj.count)")
    return arrayScanObj
}

CONSOLE (when return value is place after the completion handler):

 code: 104
 arrayScanObj.count PRE: 1
 arrayScanObj.count PRE: 2
 arrayScanObj.count PRE: 3
johnDoe
  • 709
  • 11
  • 29
  • I think this question was incorrectly close because as I see it the answer is that the `DispatchQueue.main.async` call is unnecessary and everything can be done in the current thread – Joakim Danielson Aug 26 '18 at 09:01
  • No, the duplicate is correct. They are using an asynchronous network operation. The OP has tried to return data from outside the completion handler; which gives an empty array and they have also tried to call `return` from within the completion handler, which gives the error about returning from a void function. `return` is not the solution here; they need to use a completion handler as per the answers in the duplicate. – Paulw11 Aug 26 '18 at 09:10
  • @Paulw11 so in my function decelaration I should not have a return type? – johnDoe Aug 26 '18 at 09:13
  • No, you can't return data from a function that initiates an asynchronous operation; You will need to pass a completion handler and invoke that once you have the data – Paulw11 Aug 26 '18 at 09:21
  • @Paulw11, I see that now but I still think the use of `DispatchQueue.main.async` is pointless here. – Joakim Danielson Aug 26 '18 at 09:32
  • Well, it isn't necessary but it isn't causing the problem and removing it won't change the problem they are facing. The solution to their problem is in the duplicate, – Paulw11 Aug 26 '18 at 09:34

0 Answers0