forked from loafle/openapi-generator-original
[Swift3] Use thread safe manager dictionary (#5610)
This commit is contained in:
parent
54e0492598
commit
97a4bacc4e
@ -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()
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
2.3.0-SNAPSHOT
|
@ -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()
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
2.3.0-SNAPSHOT
|
@ -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()
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
2.3.0-SNAPSHOT
|
@ -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()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user