0

I have a table view with two sections

These two sections contains data from two different urls

Im using Alamofire to download content from json.

Url from section 1 has more content than section 2. Both tasks are runnings asynchronously

Once section 2 data has been completed, im reloading table view. But for some reason my section1 data gets terminatted and no results are displayed

Please let me know how to handle that. Below is the sample code structure

let url1 = "https://xxxxxxxxxx.com"
Alamofire.request(url1)
    .responseJSON { response in
        guard let json = response.result.value as? [Any] else {}
        //do parsing from json
        mytableview.reloadData()
    }

let url2 = "https://yyyyyyyyy.com"
Alamofire.request(url2)
    .responseJSON { response in
        guard let json = response.result.value as? [Any] else {}
        //do parsing from json
        mytableview.reloadData()
    }

In my case, data from url2 is fetching and parsing json fast and is reloadingtableview. While doing my url1 is still parsing data

How can I display both url1, url2 data at a time after all the parsing done. Please advice

1 Answer 1

1

Use a DispatchGroup:

let group = DispatchGroup()

let url1 = "https://xxxxxxxxxx.com"
group.enter()
Alamofire.request(url1)
    .responseJSON { response in
        guard let json = response.result.value as? [Any] else {}
        //do parsing from json
        group.leave()
}

let url2 = "https://yyyyyyyyy.com"
group.enter()
Alamofire.request(url2)
    .responseJSON { response in
        guard let json = response.result.value as? [Any] else {}
        //do parsing from json
        group.leave()
}

group.notify(queue: DispatchQueue.main) {
    mytableview.reloadData()
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.