[Swift3] Use thread safe manager dictionary (#5610)

This commit is contained in:
Jason Gavris 2017-05-17 11:35:49 -04:00 committed by wing328
parent 54e0492598
commit 97a4bacc4e
7 changed files with 131 additions and 8 deletions

View File

@ -13,8 +13,38 @@ class AlamofireRequestBuilderFactory: RequestBuilderFactory {
} }
} }
private struct SynchronizedDictionary<K: Hashable, V> {
private var dictionary = [K: V]()
private let queue = DispatchQueue(
label: "SynchronizedDictionary",
qos: DispatchQoS.userInitiated,
attributes: [DispatchQueue.Attributes.concurrent],
autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit,
target: nil
)
public subscript(key: K) -> V? {
get {
var value: V?
queue.sync {
value = self.dictionary[key]
}
return value
}
set {
queue.sync(flags: DispatchWorkItemFlags.barrier) {
self.dictionary[key] = newValue
}
}
}
}
// Store manager to retain its reference // Store manager to retain its reference
private var managerStore: [String: Alamofire.SessionManager] = [:] private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManager>()
open class AlamofireRequestBuilder<T>: RequestBuilder<T> { open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
required public init(method: String, URLString: String, parameters: [String : Any]?, isBody: Bool, headers: [String : String] = [:]) { required public init(method: String, URLString: String, parameters: [String : Any]?, isBody: Bool, headers: [String : String] = [:]) {
@ -112,7 +142,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
} }
let cleanupRequest = { let cleanupRequest = {
_ = managerStore.removeValue(forKey: managerId) managerStore[managerId] = nil
} }
let validatedRequest = request.validate() let validatedRequest = request.validate()

View File

@ -0,0 +1 @@
2.3.0-SNAPSHOT

View File

@ -13,8 +13,38 @@ class AlamofireRequestBuilderFactory: RequestBuilderFactory {
} }
} }
private struct SynchronizedDictionary<K: Hashable, V> {
private var dictionary = [K: V]()
private let queue = DispatchQueue(
label: "SynchronizedDictionary",
qos: DispatchQoS.userInitiated,
attributes: [DispatchQueue.Attributes.concurrent],
autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit,
target: nil
)
public subscript(key: K) -> V? {
get {
var value: V?
queue.sync {
value = self.dictionary[key]
}
return value
}
set {
queue.sync(flags: DispatchWorkItemFlags.barrier) {
self.dictionary[key] = newValue
}
}
}
}
// Store manager to retain its reference // Store manager to retain its reference
private var managerStore: [String: Alamofire.SessionManager] = [:] private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManager>()
open class AlamofireRequestBuilder<T>: RequestBuilder<T> { open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
required public init(method: String, URLString: String, parameters: [String : Any]?, isBody: Bool, headers: [String : String] = [:]) { required public init(method: String, URLString: String, parameters: [String : Any]?, isBody: Bool, headers: [String : String] = [:]) {
@ -112,7 +142,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
} }
let cleanupRequest = { let cleanupRequest = {
_ = managerStore.removeValue(forKey: managerId) managerStore[managerId] = nil
} }
let validatedRequest = request.validate() let validatedRequest = request.validate()

View File

@ -0,0 +1 @@
2.3.0-SNAPSHOT

View File

@ -13,8 +13,38 @@ class AlamofireRequestBuilderFactory: RequestBuilderFactory {
} }
} }
private struct SynchronizedDictionary<K: Hashable, V> {
private var dictionary = [K: V]()
private let queue = DispatchQueue(
label: "SynchronizedDictionary",
qos: DispatchQoS.userInitiated,
attributes: [DispatchQueue.Attributes.concurrent],
autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit,
target: nil
)
public subscript(key: K) -> V? {
get {
var value: V?
queue.sync {
value = self.dictionary[key]
}
return value
}
set {
queue.sync(flags: DispatchWorkItemFlags.barrier) {
self.dictionary[key] = newValue
}
}
}
}
// Store manager to retain its reference // Store manager to retain its reference
private var managerStore: [String: Alamofire.SessionManager] = [:] private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManager>()
open class AlamofireRequestBuilder<T>: RequestBuilder<T> { open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
required public init(method: String, URLString: String, parameters: [String : Any]?, isBody: Bool, headers: [String : String] = [:]) { required public init(method: String, URLString: String, parameters: [String : Any]?, isBody: Bool, headers: [String : String] = [:]) {
@ -112,7 +142,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
} }
let cleanupRequest = { let cleanupRequest = {
_ = managerStore.removeValue(forKey: managerId) managerStore[managerId] = nil
} }
let validatedRequest = request.validate() let validatedRequest = request.validate()

View File

@ -0,0 +1 @@
2.3.0-SNAPSHOT

View File

@ -13,8 +13,38 @@ class AlamofireRequestBuilderFactory: RequestBuilderFactory {
} }
} }
private struct SynchronizedDictionary<K: Hashable, V> {
private var dictionary = [K: V]()
private let queue = DispatchQueue(
label: "SynchronizedDictionary",
qos: DispatchQoS.userInitiated,
attributes: [DispatchQueue.Attributes.concurrent],
autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit,
target: nil
)
public subscript(key: K) -> V? {
get {
var value: V?
queue.sync {
value = self.dictionary[key]
}
return value
}
set {
queue.sync(flags: DispatchWorkItemFlags.barrier) {
self.dictionary[key] = newValue
}
}
}
}
// Store manager to retain its reference // Store manager to retain its reference
private var managerStore: [String: Alamofire.SessionManager] = [:] private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManager>()
open class AlamofireRequestBuilder<T>: RequestBuilder<T> { open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
required public init(method: String, URLString: String, parameters: [String : Any]?, isBody: Bool, headers: [String : String] = [:]) { required public init(method: String, URLString: String, parameters: [String : Any]?, isBody: Bool, headers: [String : String] = [:]) {
@ -112,7 +142,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
} }
let cleanupRequest = { let cleanupRequest = {
_ = managerStore.removeValue(forKey: managerId) managerStore[managerId] = nil
} }
let validatedRequest = request.validate() let validatedRequest = request.validate()