Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add autoreleasepool around sendNextEventFile #232

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 37 additions & 35 deletions Sources/Amplitude/Utilities/EventPipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,46 +67,48 @@ public class EventPipeline {
}

private func sendNextEventFile() {
guard currentUpload == nil else {
logger?.log(message: "Existing upload in progress, skipping...")
return
}
autoreleasepool {
guard currentUpload == nil else {
logger?.log(message: "Existing upload in progress, skipping...")
return
}

guard let storage = storage,
let eventFiles: [URL] = storage.read(key: StorageKey.EVENTS),
let nextEventFile = eventFiles.first else {
flushCompletions.forEach { $0() }
flushCompletions.removeAll()
logger?.debug(message: "No event files to upload")
return
}
guard let storage = storage,
let eventFiles: [URL] = storage.read(key: StorageKey.EVENTS),
let nextEventFile = eventFiles.first else {
flushCompletions.forEach { $0() }
flushCompletions.removeAll()
logger?.debug(message: "No event files to upload")
return
}

guard configuration.offline != true else {
logger?.debug(message: "Skipping flush while offline.")
return
}
guard configuration.offline != true else {
logger?.debug(message: "Skipping flush while offline.")
return
}

guard let eventsString = storage.getEventsString(eventBlock: nextEventFile),
!eventsString.isEmpty else {
logger?.log(message: "Could not read events file: \(nextEventFile)")
return
}
guard let eventsString = storage.getEventsString(eventBlock: nextEventFile),
!eventsString.isEmpty else {
logger?.log(message: "Could not read events file: \(nextEventFile)")
return
}

currentUpload = httpClient.upload(events: eventsString) { [self] result in
let responseHandler = storage.getResponseHandler(
configuration: self.configuration,
eventPipeline: self,
eventBlock: nextEventFile,
eventsString: eventsString
)
responseHandler.handle(result: result)
// Don't send the next event file if we're being deallocated
self.uploadsQueue.async { [weak self] in
guard let self = self else {
return
currentUpload = httpClient.upload(events: eventsString) { [self] result in
let responseHandler = storage.getResponseHandler(
configuration: self.configuration,
eventPipeline: self,
eventBlock: nextEventFile,
eventsString: eventsString
)
responseHandler.handle(result: result)
// Don't send the next event file if we're being deallocated
self.uploadsQueue.async { [weak self] in
guard let self = self else {
return
}
self.currentUpload = nil
self.sendNextEventFile()
}
self.currentUpload = nil
self.sendNextEventFile()
}
}
}
Expand Down
Loading