From 49b989f5293b2301f4bcc3cc79699d94cb1b3018 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Fri, 9 Apr 2021 09:47:02 +1000 Subject: [PATCH] Fix job ID uniqueness --- SessionMessagingKit/Jobs/JobQueue.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/SessionMessagingKit/Jobs/JobQueue.swift b/SessionMessagingKit/Jobs/JobQueue.swift index 16168f52c..a09bc5cfb 100644 --- a/SessionMessagingKit/Jobs/JobQueue.swift +++ b/SessionMessagingKit/Jobs/JobQueue.swift @@ -3,6 +3,8 @@ import SessionUtilitiesKit @objc(SNJobQueue) public final class JobQueue : NSObject, JobDelegate { + private static var jobIDs: [UInt64:UInt64] = [:] + @objc public static let shared = JobQueue() @objc public func add(_ job: Job, using transaction: Any) { @@ -14,7 +16,15 @@ public final class JobQueue : NSObject, JobDelegate { } @objc public func addWithoutExecuting(_ job: Job, using transaction: Any) { - job.id = String(NSDate.millisecondTimestamp()) + let timestamp = NSDate.millisecondTimestamp() + let count = JobQueue.jobIDs[timestamp] ?? 0 + // When adding multiple jobs in rapid succession, timestamps might not be good enough as a unique ID. To + // deal with this we keep track of the number of jobs with a given timestamp and that to the end of the + // timestamp to make it a unique ID. We can't use a random number because we do still want to keep track + // of the order in which the jobs were added. + let id = String(timestamp) + String(count) + job.id = id + JobQueue.jobIDs[timestamp] = count + 1 SNMessagingKitConfiguration.shared.storage.persist(job, using: transaction) job.delegate = self }