Thursday, 22 August 2019

Chaining Multiple JSON Request Using Decodable - Swift 5

My "url" object has a link that captures what the user types into a search bar to complete the link then begin the JSON process. The first link responds with another link after the JSON has finished parsing. In my if let validLink = result.link you see I store the link information into an Array. Now I'm not sure if I should begin another JSON response in my if let validLink = result or if I should create a new function like I'm attempting to do in the code below and basically copied and pasted the same JSON information below to reparse it. The second link is getting a parse error. What is the most efficient and right way to do this? I'm really stuck here.

I've tried to create another function that uses the information from the first JSON parse to reparse again using the new link.

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()

    if let searchText = searchController.searchBar.text, !searchText.isEmpty {
        let url = URL(string: "http://djp-dev/api/item?q=\(String(describing: searchText))&dev=1")
        let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
                guard let data = data,
                    error == nil else {
                        print(error?.localizedDescription ?? "Response Error")
                        return }

                do {
                    let jsonResult = try JSONDecoder().decode(Response.self, from: data)
                    let resultsArray = jsonResult.results

                    for result in resultsArray {
                        if let validLink = result.link {
                            print(validLink)
                            self.collectLink.append(validLink)
                            self.mainParse()
                        }
                    }

                } catch {
                    print("Parse Error")
            }
        }
         task.resume()
    }
}

func mainParse() {
   let url = URL(string: "http://djp-dev\(collectLink[0])?dev=1")
    print(url!)
    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
        guard let data = data,
            error == nil else {
                //print(error?.localizedDescription ?? "Response Error")
                return }

            do {
                let jsonResult = try JSONDecoder().decode(JSONResponse.self, from: data)
                let mainArray = jsonResult.locations

                for main in mainArray {
                   print("""
                        Manufacture = \(main.rid)
                        Description = \(main.description)
                        """)
                        /*if let validLink = result.description! {
                        }*/
                    }

                } catch {
                    print("Parse Error")
            }
        }
            task.resume()

    DispatchQueue.main.async {
            self.tableView.reloadData()
    }

}

I basically ask http://djp-dev/api/item?q=\(String(describing: searchText))&dev=1 for a link in the response it sends me back. I want the use the link it sends me to start another JSON request. I'm not sure if I should keep it all into one request or create a whole new function with a whole new JSON request. if let validLink = result.link { } is when I receive the second link information.



from Chaining Multiple JSON Request Using Decodable - Swift 5

No comments:

Post a Comment