I am creating an application where I need to load data into pieces, I mean first load 5 elements, and then go to another 5, but I canโt figure out how to do this. At the moment, I am breaking a list of my items, so I get a list of lists of 5 pieces each. For now, for-loop just starts with requests, but I want to wait for an answer and then go into the for loop.
I am using alamofire and my code looks like this.
private func requestItemsForField(items: [Item], completion: @escaping (_ measurements: Array<Measurement>?, _ success: Bool) -> ()) { let userPackageId = UserManager.instance.selectedUserPackage.id let params = ["userPackageId": userPackageId] for field in fields { let url = apiURL + "images/\(field.id)" let queue = DispatchQueue(label: "com.response-queue", qos: .utility, attributes: [.concurrent]) Alamofire.request(url, method: .get, parameters: params, headers: headers()).responseArray(queue: queue, completionHandler: { (response: DataResponse<[Item]>) in if let items = response.result.value as [Item]? { NotificationCenter.default.post(name: NSNotification.Name(rawValue: "itemsLoadedNotification"), object: nil) completion(items, true) } else { print("Request failed with error: \(response.result.error)") completion(nil, false) } }) } }
Here I break up my list and pass it above.
private func fetchAllMeasurements(completion: @escaping (_ measurements: [Item]?, _ done: Bool) -> ()) { let fieldSet = FieldStore.instance.data.keys var fieldKeys = [Item]() for field in fieldSet { fieldKeys.append(field) } // Create chunks of fields to load let fieldChunks = fieldKeys.chunkify(by: 5) var measurementsAll = [Measurement]() for fields in fieldChunks { requestItemsForField(fields: fields, completion: { (measurements, success) in if let currentMeasurement = measurements { measurementsAll.append(contentsOf: currentMeasurement) } completion(measurementsAll, true) } }) } }
source share