0

I would like my app to wait until the request results to come back. So in below block, what I'm trying to do is if the requested query returns empty results, I'd like to call "self.insertCreditRecord". Has anyone done this? appSyncClient?.fetch(query: selectQuery, cachePolicy: .fetchIgnoringCacheData) {(result, error) in if error != nil { print(error?.localizedDescription ?? "") return }

            result?.data?.listTranscriberConfigs?.items!.forEach {

                if let credit = Double($0?.value ?? "0.0")
                {
                    UserCredit.id = ""
                    UserCredit.id = $0?.id ?? ""
                    UserCredit.email = $0?.param ?? ""
                    UserCredit.credit =  credit
                }

                if UserCredit.id == "" {
                    self.insertCreditRecord(email: email)
                }
                print(($0?.param)! + " " + ($0?.value)!)
 
            }
        
        
        
    }

1 Answers1

1

you just need to use completion handler in your function to ignore the null return

For Example

typealias CompletionHandler = (success:Bool) -> Void

func downloadFileFromURL(url: NSURL,completionHandler: CompletionHandler) {

    // download code.

    let flag = true // true if download succeed,false otherwise

    completionHandler(success: flag)
}

// How to use it.

downloadFileFromURL(NSURL(string: "url_str")!, { (success) -> Void in

    // When download completes,control flow goes here.
    if success {
        // download success
    } else {
        // download fail
    }
})
saqib kafeel
  • 296
  • 3
  • 8