How would I prevent a for loop from firing more than 10 times every second?

Viewed 78

I have created a request to OnRadar using an array of information to create geofences. However, my attempts to create a time/delay do not prevent the for loop from firing more than ten times per second, which the API cannot handle.

func setUpGeofences(){

    for object in geofenceArray{

      let params: [String: Any] = [
          "description": object.name,
          "tag": object.tag,
          "externalId": object.externalID,
          "type": "circle",
          "coordinates": [object.lat, object.long],
          "radius": object.radius,
          ]

      let headers = ["Authorization: Key"]
      let rq = Alamofire.request("https://www.onradar.com/api/v1/geofences", method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: headers)
          .responseJSON { response in

              print(response.result.value)
              print(response.result.error)
          }
    }
}

This is my function with the for Loop. I've had a look at a few examples but when I tried to apply them to my own work, I was unsuccessful. Furthermore, how would I stop the loop from running once it has created geofences for each location in my array?

2 Answers
Related