Skip to content

Commit 9b7d189

Browse files
committed
Don't batch custom events
1 parent 8edc8fb commit 9b7d189

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

iOS_SDK/OneSignalSDK/OneSignalUser/Source/Executors/OSCustomEventsExecutor.swift

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ class OSCustomEventsExecutor: OSOperationExecutor {
117117
}
118118

119119
/// The `deltaQueue` can contain events for multiple users. They will remain as Deltas if there is no onesignal ID yet for its user.
120-
func processDeltaQueue(inBackground: Bool) {
120+
/// This method will be used in an upcoming release that combine multiple events.
121+
func processDeltaQueueWithBatching(inBackground: Bool) {
121122
self.dispatchQueue.async {
122123
if self.deltaQueue.isEmpty {
123124
// Delta queue is empty but there may be pending requests
@@ -179,6 +180,55 @@ class OSCustomEventsExecutor: OSOperationExecutor {
179180
}
180181
}
181182

183+
func processDeltaQueue(inBackground: Bool) {
184+
self.dispatchQueue.async {
185+
if self.deltaQueue.isEmpty {
186+
// Delta queue is empty but there may be pending requests
187+
self.processRequestQueue(inBackground: inBackground)
188+
return
189+
}
190+
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OSCustomEventsExecutor processDeltaQueue with queue: \(self.deltaQueue)")
191+
192+
for (index, delta) in self.deltaQueue.enumerated().reversed() {
193+
guard let identityModel = OneSignalUserManagerImpl.sharedInstance.getIdentityModel(delta.identityModelId),
194+
let onesignalId = identityModel.onesignalId
195+
else {
196+
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OSCustomEventsExecutor.processDeltaQueue skipping: \(delta)")
197+
// keep this Delta in the queue, as it is not yet ready to be processed
198+
continue
199+
}
200+
201+
guard let properties = delta.value as? [String: Any] else {
202+
// This should not happen as there are preventative typing measures before this step
203+
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSCustomEventsExecutor.processDeltaQueue dropped due to invalid properties: \(delta)")
204+
self.deltaQueue.remove(at: index)
205+
continue
206+
}
207+
208+
let event: [String: Any] = [
209+
EventConstants.name: delta.property,
210+
EventConstants.onesignalId: onesignalId,
211+
EventConstants.timestamp: ISO8601DateFormatter().string(from: delta.timestamp),
212+
EventConstants.payload: self.addSdkMetadata(properties: properties)
213+
]
214+
215+
self.deltaQueue.remove(at: index)
216+
217+
let request = OSRequestCustomEvents(
218+
events: [event],
219+
identityModel: identityModel
220+
)
221+
self.requestQueue.append(request)
222+
}
223+
224+
// Persist executor's requests (including new request) to storage
225+
OneSignalUserDefaults.initShared().saveCodeableData(forKey: OS_CUSTOM_EVENTS_EXECUTOR_REQUEST_QUEUE_KEY, withValue: self.requestQueue)
226+
OneSignalUserDefaults.initShared().saveCodeableData(forKey: OS_CUSTOM_EVENTS_EXECUTOR_DELTA_QUEUE_KEY, withValue: self.deltaQueue)
227+
228+
self.processRequestQueue(inBackground: inBackground)
229+
}
230+
}
231+
182232
/**
183233
Adds additional data about the SDK to the event payload.
184234
*/

0 commit comments

Comments
 (0)