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 CoreData Thread Safety in retryCachedData #182

Closed
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
27 changes: 15 additions & 12 deletions Sources/EmbraceUploadInternal/EmbraceUpload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,26 @@ public class EmbraceUpload: EmbraceLogUploader {
// clear data from cache that shouldn't be retried as it's stale
self.clearCacheFromStaleData()

// get all the data cached first, is the only thing that could throw
// Use the fetchAllUploadData method which handles the CoreData context properly
let cachedObjects = try self.cache.fetchAllUploadData()

// create a sempahore to allow only to send two request at a time so we don't
// get throttled by the backend on cases where cache has many failed requests.

for uploadData in cachedObjects {
guard let type = EmbraceUploadType(rawValue: uploadData.type) else {
continue

// Create value types from managed objects
let uploadTasks = cachedObjects.map { record -> (id: String, data: Data, type: EmbraceUploadType, attemptCount: Int)? in
guard let type = EmbraceUploadType(rawValue: record.type) else {
return nil
}
return (record.id, record.data, type, record.attemptCount)
}.compactMap { $0 }

// Process the value types
for task in uploadTasks {
self.semaphore.wait()

self.reUploadData(
id: uploadData.id,
data: uploadData.data,
type: type,
attemptCount: uploadData.attemptCount
id: task.id,
data: task.data,
type: task.type,
attemptCount: task.attemptCount
) {
self.semaphore.signal()
}
Expand Down
Loading