From 353db6966302b95ed2c1afa864691c9107554926 Mon Sep 17 00:00:00 2001 From: Kaushal Kapasi Date: Mon, 1 Apr 2024 17:08:52 -0400 Subject: [PATCH] fix: batch event payloads into chunks of 100 to reduce errors on event processing --- DevCycle/Networking/DevCycleService.swift | 40 ++++++++++++++++------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/DevCycle/Networking/DevCycleService.swift b/DevCycle/Networking/DevCycleService.swift index 04d57c7..aa6bf03 100644 --- a/DevCycle/Networking/DevCycleService.swift +++ b/DevCycle/Networking/DevCycleService.swift @@ -77,6 +77,7 @@ class DevCycleService: DevCycleServiceProtocol { var requestConsolidator: RequestConsolidator! private var newUser: DevCycleUser? + private var maxBatchSize = 100 init(config: DVCConfig, cacheService: CacheServiceProtocol, options: DevCycleOptions? = nil) { let sessionConfig = URLSessionConfiguration.default @@ -105,24 +106,39 @@ class DevCycleService: DevCycleServiceProtocol { return completion((nil, nil, ClientError.InvalidUser)) } - let requestBody: [String: Any] = [ - "events": eventPayload, - "user": userBody - ] - eventsRequest.httpMethod = "POST" eventsRequest.addValue("application/json", forHTTPHeaderField: "Content-Type") eventsRequest.addValue("application/json", forHTTPHeaderField: "Accept") eventsRequest.addValue(config.sdkKey, forHTTPHeaderField: "Authorization") - let jsonBody = try? JSONSerialization.data(withJSONObject: requestBody, options: .prettyPrinted) - Log.debug("Post Events Payload: \(String(data: jsonBody!, encoding: .utf8) ?? "")") - eventsRequest.httpBody = jsonBody - self.makeRequest(request: eventsRequest) { data, response, error in - if error != nil || data == nil { - return completion((data, response, error)) + let totalEventsCount = eventPayload.count + var startIndex = 0 + var endIndex = min(self.maxBatchSize, totalEventsCount) + + while startIndex < totalEventsCount { + let batchEvents = Array(eventPayload[startIndex..= totalEventsCount { + return completion((data, response, nil)) + } } - return completion((data, response, nil)) } }