From a2c3a6fd9e2c1f0108c05d0bf9fd6d7e4220503f Mon Sep 17 00:00:00 2001 From: Lucas Nelaupe <7482647+lucas34@users.noreply.github.com> Date: Mon, 6 May 2019 19:12:35 +0800 Subject: [PATCH] Rename synchronous to initInBackground (#213) --- Sources/SwiftQueue/SqOperationQueue.swift | 8 ++++---- Sources/SwiftQueue/SwiftQueueManager.swift | 19 +++++++++++++------ Tests/SwiftQueueTests/PersisterTests.swift | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/Sources/SwiftQueue/SqOperationQueue.swift b/Sources/SwiftQueue/SqOperationQueue.swift index 445487de..f3c29b8c 100644 --- a/Sources/SwiftQueue/SqOperationQueue.swift +++ b/Sources/SwiftQueue/SqOperationQueue.swift @@ -29,7 +29,7 @@ internal final class SqOperationQueue: OperationQueue { private let trigger: Operation init(_ queueName: String, _ creator: JobCreator, _ persister: JobPersister, _ serializer: JobInfoSerializer, - _ isSuspended: Bool, _ synchronous: Bool, _ logger: SwiftQueueLogger) { + _ isSuspended: Bool, _ initInBackground: Bool, _ logger: SwiftQueueLogger) { self.queueName = queueName @@ -46,12 +46,12 @@ internal final class SqOperationQueue: OperationQueue { self.name = queueName self.maxConcurrentOperationCount = 1 - if synchronous { - self.loadSerializedTasks(name: queueName) - } else { + if initInBackground { DispatchQueue.global(qos: DispatchQoS.QoSClass.utility).async { () -> Void in self.loadSerializedTasks(name: queueName) } + } else { + self.loadSerializedTasks(name: queueName) } } diff --git a/Sources/SwiftQueue/SwiftQueueManager.swift b/Sources/SwiftQueue/SwiftQueueManager.swift index 66fa21a1..371ba7ac 100644 --- a/Sources/SwiftQueue/SwiftQueueManager.swift +++ b/Sources/SwiftQueue/SwiftQueueManager.swift @@ -47,7 +47,7 @@ public final class SwiftQueueManager { self.isSuspended = params.isSuspended for queueName in persister.restore() { - manage[queueName] = SqOperationQueue(queueName, creator, persister, serializer, isSuspended, params.synchronous, logger) + manage[queueName] = SqOperationQueue(queueName, creator, persister, serializer, isSuspended, params.initInBackground, logger) } } @@ -57,7 +57,7 @@ public final class SwiftQueueManager { private func createQueue(queueName: String) -> SqOperationQueue { // At this point the queue should be totally new so it's safe to start the queue synchronously - let queue = SqOperationQueue(queueName, creator, persister, serializer, isSuspended, true, logger) + let queue = SqOperationQueue(queueName, creator, persister, serializer, isSuspended, false, logger) manage[queueName] = queue return queue } @@ -120,21 +120,21 @@ internal class SqManagerParams { var isSuspended: Bool - var synchronous: Bool + var initInBackground: Bool init(creator: JobCreator, persister: JobPersister = UserDefaultsPersister(), serializer: JobInfoSerializer = DecodableSerializer(), logger: SwiftQueueLogger = NoLogger.shared, isSuspended: Bool = false, - synchronous: Bool = true) { + initInBackground: Bool = false) { self.creator = creator self.persister = persister self.serializer = serializer self.logger = logger self.isSuspended = isSuspended - self.synchronous = synchronous + self.initInBackground = initInBackground } } @@ -176,8 +176,15 @@ public final class SwiftQueueManagerBuilder { } /// Deserialize jobs synchronously after creating the `SwiftQueueManager` instance. `true` by default + @available(*, deprecated, renamed: "initInBackground") public func set(synchronous: Bool) -> Self { - params.synchronous = synchronous + params.initInBackground = !synchronous + return self + } + + /// Deserialize jobs synchronously after creating the `SwiftQueueManager` instance. `true` by default + public func set(initInBackground: Bool) -> Self { + params.initInBackground = initInBackground return self } diff --git a/Tests/SwiftQueueTests/PersisterTests.swift b/Tests/SwiftQueueTests/PersisterTests.swift index a70e744b..af259215 100644 --- a/Tests/SwiftQueueTests/PersisterTests.swift +++ b/Tests/SwiftQueueTests/PersisterTests.swift @@ -238,7 +238,7 @@ class PersisterTests: XCTestCase { tasks[lastTaskType] = lastJob let creator = TestCreator(tasks) - let manager = SwiftQueueManagerBuilder(creator: creator).set(persister: persister).set(synchronous: false).build() + let manager = SwiftQueueManagerBuilder(creator: creator).set(persister: persister).set(initInBackground: true).build() JobBuilder(type: lastTaskType) .singleInstance(forId: lastTaskType)