Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

arrays - Completion Handler in API never runs

I have an APICall as below

static func getLinkToken() {
        // Prepare URL
        let url = URL(string: "https://sandbox.plaid.com/link/token/create")
        guard let requestUrl = url else { fatalError() }
        // Prepare URL Request Object
        var request = URLRequest(url: requestUrl)
        request.httpMethod = "POST"
  
        let prePostString : [String : Any] = [
            "client_id" : clientID,
            "secret" : secret,
            "user" : ["client_user_id": uniqueID],
            "client_name": "Plaid App",
            "products": ["transactions"],
            "country_codes": ["US"],
            "language": "en",
            "account_filters": [
                 "depository": [
                     "account_subtypes": ["checking"]
                 ]
             ]
        ]

        let postString = try? JSONSerialization.data(withJSONObject: prePostString)
        
        
        // Set HTTP Request Body
        request.httpBody = postString;
        
        
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        
        // Perform HTTP Request
        let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
                
                // Check for Error
                if let error = error {
                    print("Error took place (error)")
                    return
                }
         
            do {
                //Parse response into a dict
                let parsedData = try JSONSerialization.jsonObject(with: data!) as! [String:Any]
                
                //Add link token to user class
                User.linkToken = (parsedData["link_token"] as! String)
                
            } catch let error as NSError {
                print(error)
              }
            
                // Convert HTTP Response Data to a String
                if let data = data, let dataString = String(data: data, encoding: .utf8) {
                    print("Response data string:
 (dataString)")
                    
                   
                    
                }
        }
        task.resume()
        
    }

For some reason this code never works if I call it unless it is in @main.init. It appears that when I call the function in any class/method other than the initializer for main, the completion handler never runs. An example of how I call it is shown below.

class Practice {

     func getAPI() {

         getLinkToken()

    }

}

TLDR; completion handler in API call never runs.

Thanks

question from:https://stackoverflow.com/questions/66066317/completion-handler-in-api-never-runs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...