Swift: Can't fetch data from API Closure - TagMerge
1Swift: Can't fetch data from API ClosureSwift: Can't fetch data from API Closure

Swift: Can't fetch data from API Closure

Asked 11 months ago
0
1 answers

as @Larme mentioned, you need to deal with the asynchronous function. Read-up about it. Try this approach:

override func viewDidLoad() {
    super.viewDidLoad()
    loaddata() { done in  // <--- here
        print(datasource) // print when all work is done
    }
}
 
func loadata(completion: @escaping(Bool) -> ()) {  // <--- here
    VenderAPI(method: "GET", endpoint: "vender", APIportocol: Vender(), index: "") {
        result in
        switch result {
        case .failure(let error):
            print(error.localizedDescription)
        case .success(let APIdata):
            for data in APIdata {
                self.datasource.append(data)
            }
        }
        completion(true)  // <--- here
    }
}
        

Source: link

Recent Questions on swift

    Programming Languages