Last active
December 31, 2017 11:17
-
-
Save Yerazhas/0a5f145d26084dfef2a8d2579fad6da8 to your computer and use it in GitHub Desktop.
DispatchGroup, same request with different parameters inside loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let group = DispatchGroup() | |
| var items = [ProductViewModelItem]() | |
| func getData(with data: [String: Int], completion: @escaping () -> Void) { | |
| let queue = DispatchQueue(label: "serialQ") | |
| for (_, value) in data { | |
| makeRequest(with: value, queue: queue) | |
| } | |
| group.notify(queue: DispatchQueue.main) { | |
| completion() | |
| } | |
| } | |
| func makeRequest(with data: Int, queue: DispatchQueue) { | |
| group.enter() | |
| Alamofire.request(Endpoints.getProductsBy(categoryId: data).path).responseJSON(queue: queue, options: .allowFragments) { (response) in | |
| guard let json = response.result.value, | |
| let jsonData = JSON(json)["data"].arrayObject as? [[String: AnyObject]] | |
| else { | |
| print("not parsing anything") | |
| return } | |
| var products = [Product]() | |
| for dictionary in jsonData { | |
| let product = Product() | |
| guard let name = dictionary["producName"] as? String, | |
| let image = dictionary["img_lg"] as? String, | |
| let price = dictionary["producPrice"] as? String, | |
| let description = dictionary["description"] as? String | |
| else { return } | |
| product.name = name | |
| product.image = image | |
| product.price = price | |
| product.description = description | |
| products.append(product) | |
| } | |
| let item = ProductViewModelItem(section: Section(name: "123", products: products)) | |
| for product in item.products { | |
| print(product.name) | |
| } | |
| self.items.append(item) | |
| self.group.leave() | |
| print("parsing objects") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment