diff --git a/PostHog/PostHogFileBackedQueue.swift b/PostHog/PostHogFileBackedQueue.swift index 24adf0582..ab9160709 100644 --- a/PostHog/PostHogFileBackedQueue.swift +++ b/PostHog/PostHogFileBackedQueue.swift @@ -100,10 +100,14 @@ class PostHogFileBackedQueue { private func deleteFiles(_ count: Int) { for _ in 0 ..< count { - if items.isEmpty { return } - let removed = items.remove(at: 0) // We always remove from the top of the queue - - deleteSafely(queue.appendingPathComponent(removed)) + if let removed: String = _items.mutate({ items in + if items.isEmpty { + return nil + } + return items.remove(at: 0) // We always remove from the top of the queue + }) { + deleteSafely(queue.appendingPathComponent(removed)) + } } } } diff --git a/PostHog/Utils/ReadWriteLock.swift b/PostHog/Utils/ReadWriteLock.swift index cd7d05934..c782a15f0 100644 --- a/PostHog/Utils/ReadWriteLock.swift +++ b/PostHog/Utils/ReadWriteLock.swift @@ -50,10 +50,13 @@ public final class ReadWriteLock { /// The lock will be acquired once for writing before invoking the closure. /// /// - Parameter closure: The closure with the mutable value. - public func mutate(_ closure: (inout Value) -> Void) { + @discardableResult + public func mutate(_ closure: (inout Value) -> T) -> T { pthread_rwlock_wrlock(&rwlock) - closure(&value) - pthread_rwlock_unlock(&rwlock) + defer { + pthread_rwlock_unlock(&rwlock) + } + return closure(&value) } }