Skip to content

Commit 8100752

Browse files
committed
Add contains and cancel requests for Request.Type. Rename cancelRequest to cancelRequests. Fix bug - don't removed requestId after request finished.
1 parent 1b04b5f commit 8100752

File tree

2 files changed

+84
-40
lines changed

2 files changed

+84
-40
lines changed

Source/WebService.swift

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// WebServiceSwift 2.2.1
44
//
55
// Created by ViR (Короткий Виталий) on 14.06.2017.
6-
// Updated to 2.2.1 by ViR (Короткий Виталий) on 19.05.2018.
6+
// Updated to 2.2.1 by ViR (Короткий Виталий) on 20.05.2018.
77
// Copyright © 2017 ProVir. All rights reserved.
88
//
99

@@ -90,6 +90,7 @@ public class WebService {
9090
private let storages: [WebServiceStoraging]
9191

9292
private var requestList: [AnyHashable: Set<UInt64>] = [:]
93+
private var requestTypes: [String: Set<UInt64>] = [:] //[Request.Type: [Id]]
9394
private var requestUseEngines: [UInt64: WebServiceEngining] = [:]
9495

9596

@@ -138,6 +139,16 @@ public class WebService {
138139
return (internalListRequest(requestKeyType: requestKeyType, onlyFirst: true)?.count ?? 0) > 0
139140
}
140141

142+
/**
143+
Returns a Boolean value indicating whether the current queue contains the given requests.
144+
145+
- Parameter requestType: The type request to find in the all current queue.
146+
- Returns: `true` if one request with WebServiceBaseRequesting.Type was found in the current queue; otherwise, `false`.
147+
*/
148+
public func containsRequest(requestType: WebServiceBaseRequesting.Type) -> Bool {
149+
return (internalListRequest(requestType: requestType)?.count ?? 0) > 0
150+
}
151+
141152

142153
/**
143154
Cancel all requests with equal this request.
@@ -146,8 +157,8 @@ public class WebService {
146157

147158
- Parameter request: The request to find in the current queue.
148159
*/
149-
public func cancelRequest(request: WebServiceBaseRequesting) {
150-
cancelRequest(requestKey: request.requestKey)
160+
public func cancelRequests(request: WebServiceBaseRequesting) {
161+
cancelRequests(requestKey: request.requestKey)
151162
}
152163

153164
/**
@@ -157,7 +168,7 @@ public class WebService {
157168

158169
- Parameter requestKey: The requestKey to find in the current queue.
159170
*/
160-
public func cancelRequest(requestKey: AnyHashable?) {
171+
public func cancelRequests(requestKey: AnyHashable?) {
161172
if let list = internalListRequest(requestKey: requestKey) {
162173
internalCancelRequests(ids: list)
163174
}
@@ -170,12 +181,25 @@ public class WebService {
170181

171182
- Parameter requestKeyType: The requestKey.Type to find in the current queue.
172183
*/
173-
public func cancelRequest<T: Hashable>(requestKeyType: T.Type) {
184+
public func cancelRequests<T: Hashable>(requestKeyType: T.Type) {
174185
if let list = internalListRequest(requestKeyType: requestKeyType, onlyFirst: false) {
175186
internalCancelRequests(ids: list)
176187
}
177188
}
178189

190+
/**
191+
Cancel all requests for request type.
192+
193+
Signal cancel send to engine, but real canceled implementation in engine.
194+
195+
- Parameter requestType: The WebServiceBaseRequesting.Type to find in the current queue.
196+
*/
197+
public func cancelRequests(requestType: WebServiceBaseRequesting.Type) {
198+
if let list = internalListRequest(requestType: requestType) {
199+
internalCancelRequests(ids: list)
200+
}
201+
}
202+
179203
/**
180204
Cancel all requests in current queue.
181205

@@ -242,8 +266,9 @@ public class WebService {
242266

243267
let storage = internalFindStorage(request: request)
244268

269+
let requestType = type(of: request)
245270
let requestId = internalNewRequestId()
246-
internalAddRequest(requestId: requestId, requestKey: requestKey, engine: engine)
271+
internalAddRequest(requestId: requestId, requestKey: requestKey, requestType: requestType, engine: engine)
247272

248273
//Request in work
249274
var requestStatus = RequestStatus.inWork
@@ -254,7 +279,7 @@ public class WebService {
254279
queueForResponse.async {
255280
guard requestStatus == .inWork else { return }
256281

257-
self?.internalRemoveRequest(requestId: requestId, requestKey: requestKey)
282+
self?.internalRemoveRequest(requestId: requestId, requestKey: requestKey, requestType: requestType)
258283

259284
switch response {
260285
case .data(let data):
@@ -525,7 +550,7 @@ public class WebService {
525550
return WebService.lastRequestId
526551
}
527552

528-
private func internalAddRequest(requestId: UInt64, requestKey: AnyHashable?, engine: WebServiceEngining) {
553+
private func internalAddRequest(requestId: UInt64, requestKey: AnyHashable?, requestType: WebServiceBaseRequesting.Type, engine: WebServiceEngining) {
529554
//Increment counts for visible NetworkActivityIndicator in StatusBar if need only for iOS
530555
#if os(iOS)
531556
if engine.useNetworkActivityIndicator {
@@ -539,16 +564,18 @@ public class WebService {
539564
mutex.lock()
540565
defer { mutex.unlock() }
541566

567+
//1. request list
542568
let key = requestKey ?? AnyHashable(EmptyKey())
569+
requestList[key, default: Set<UInt64>()].insert(requestId)
543570

544-
var list = requestList[key] ?? Set<UInt64>()
545-
list.insert(requestId)
546-
requestList[key] = list
547-
571+
//2. use engines
548572
requestUseEngines[requestId] = engine
573+
574+
//3. types requests
575+
requestTypes["\(requestType)", default: Set<UInt64>()].insert(requestId)
549576
}
550577

551-
private func internalRemoveRequest(requestId: UInt64, requestKey: AnyHashable?) {
578+
private func internalRemoveRequest(requestId: UInt64, requestKey: AnyHashable?, requestType: WebServiceBaseRequesting.Type) {
552579
WebService.staticMutex.lock()
553580
WebService.networkActivityIndicatorRequestIds.remove(requestId)
554581
WebService.staticMutex.unlock()
@@ -557,17 +584,18 @@ public class WebService {
557584
mutex.lock()
558585
defer { mutex.unlock() }
559586

587+
//1. request list
560588
let key = requestKey ?? AnyHashable(EmptyKey())
589+
requestList[key]?.remove(requestId)
590+
if requestList[key]?.isEmpty ?? false { requestList.removeValue(forKey: key) }
561591

562-
if var list = requestList[key] {
563-
list.remove(requestId)
564-
565-
if list.isEmpty {
566-
requestList.removeValue(forKey: key)
567-
}
568-
}
569-
592+
//2. use engines
570593
requestUseEngines.removeValue(forKey: requestId)
594+
595+
//3. types requests
596+
let typeKey = "\(requestType)"
597+
requestTypes[typeKey]?.remove(requestId)
598+
if requestTypes[typeKey]?.isEmpty ?? false { requestTypes.removeValue(forKey: typeKey) }
571599
}
572600

573601
private func internalListRequest(requestKey: AnyHashable?) -> Set<UInt64>? {
@@ -597,6 +625,14 @@ public class WebService {
597625
return ids.isEmpty ? nil : ids
598626
}
599627

628+
private func internalListRequest(requestType: WebServiceBaseRequesting.Type) -> Set<UInt64>? {
629+
mutex.lock()
630+
defer { mutex.unlock() }
631+
632+
let key = "\(requestType)"
633+
return requestTypes[key]
634+
}
635+
600636
private func internalCancelRequests(ids: Set<UInt64>) {
601637
for requestId in ids {
602638
if let engine = mutex.synchronized({ self.requestUseEngines[requestId] }) {

Source/WebServiceRequestProvider.swift

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
//
22
// WebServiceRequestProvider.swift
3-
// WebServiceSwift 2.2.0
3+
// WebServiceSwift 2.2.1
44
//
55
// Created by ViR (Короткий Виталий) on 24.04.2018.
6-
// Updated to 2.2.0 by ViR (Короткий Виталий) on 16.05.2018.
6+
// Updated to 2.2.1 by ViR (Короткий Виталий) on 20.05.2018.
77
// Copyright © 2018 ProVir. All rights reserved.
88
//
99

1010
import Foundation
1111

1212
/// Provider for single request type.
1313
public class WebServiceRequestProvider<RequestType: WebServiceRequesting>: WebServiceProvider {
14-
private let service: WebService
14+
fileprivate let service: WebService
1515

1616
public required init(webService: WebService) {
1717
self.service = webService
@@ -43,15 +43,24 @@ public class WebServiceRequestProvider<RequestType: WebServiceRequesting>: WebSe
4343
return service.containsRequest(requestKey: requestKey)
4444
}
4545

46+
/**
47+
Returns a Boolean value indicating whether the current queue contains the given requests.
48+
49+
- Returns: `true` if one request with current type was found in the current queue; otherwise, `false`.
50+
*/
51+
public func containsRequests() -> Bool {
52+
return service.containsRequest(requestType: RequestType.self)
53+
}
54+
4655
/**
4756
Cancel all requests with equal this request.
4857

4958
Signal cancel send to engine, but real canceled implementation in engine.
5059

5160
- Parameter request: The request to find in the current queue.
5261
*/
53-
public func cancelRequest(request: RequestType) {
54-
service.cancelRequest(request: request)
62+
public func cancelRequests(request: RequestType) {
63+
service.cancelRequests(request: request)
5564
}
5665

5766
/**
@@ -61,18 +70,17 @@ public class WebServiceRequestProvider<RequestType: WebServiceRequesting>: WebSe
6170

6271
- Parameter requestKey: The requestKey to find in the current queue.
6372
*/
64-
public func cancelRequest(requestKey: AnyHashable?) {
65-
service.cancelRequest(requestKey: requestKey)
73+
public func cancelRequests(requestKey: AnyHashable?) {
74+
service.cancelRequests(requestKey: requestKey)
6675
}
6776

6877
/**
69-
Cancel all requests in current queue.
70-
78+
Cancel all requests current type in current queue.
79+
7180
Signal cancel send to engine, but real canceled implementation in engine.
72-
Warning: cancel all requests from webService for other Helper with this webService.
7381
*/
7482
public func cancelAllRequests() {
75-
service.cancelAllRequests()
83+
service.cancelRequests(requestType: RequestType.self)
7684
}
7785

7886

@@ -82,9 +90,9 @@ public class WebServiceRequestProvider<RequestType: WebServiceRequesting>: WebSe
8290
Request for server (and to storage, if need). Response result in closure.
8391

8492
- Parameters:
85-
- request: The request data.
86-
- dataFromStorage: Optional. Closure for read data from storage. if read data after data from server - cloure not call. If `closure == nil`, data not read from storage.
87-
- completionResponse: Optional. Closure for response result from server.
93+
- request: The request data.
94+
- dataFromStorage: Optional. Closure for read data from storage. if read data after data from server - cloure not call. If `closure == nil`, data not read from storage.
95+
- completionResponse: Optional. Closure for response result from server.
8896
*/
8997
public func performRequest(_ request: RequestType, dataFromStorage: ((_ data: RequestType.ResultType) -> Void)? = nil, completionResponse: @escaping (_ response: WebServiceResponse<RequestType.ResultType>) -> Void) {
9098
service.performRequest(request, dataFromStorage: dataFromStorage, completionResponse: completionResponse)
@@ -94,9 +102,9 @@ public class WebServiceRequestProvider<RequestType: WebServiceRequesting>: WebSe
94102
Request for only storage. Response result in closure.
95103

96104
- Parameters:
97-
- request: The request data.
98-
- completionResponse: Closure for read data from storage.
99-
- response: result read from storage.
105+
- request: The request data.
106+
- completionResponse: Closure for read data from storage.
107+
- response: result read from storage.
100108
*/
101109
public func readStorage(_ request: RequestType, completionResponse: @escaping (_ response: WebServiceResponse<RequestType.ResultType>) -> Void) {
102110
service.readStorage(request, completionResponse: completionResponse)
@@ -109,8 +117,8 @@ public class WebServiceRequestProvider<RequestType: WebServiceRequesting>: WebSe
109117
Request for server (and to storage, if need). Response result for delegate in helper or `WebService.delegate`.
110118

111119
- Parameters:
112-
- request: The request data.
113-
- includeResponseStorage: `true` if need read data from storage. if read data after data from server - delegate not call. Default: false.
120+
- request: The request data.
121+
- includeResponseStorage: `true` if need read data from storage. if read data after data from server - delegate not call. Default: false.
114122
*/
115123
public func performRequest(_ request: RequestType, includeResponseStorage: Bool = false) {
116124
service.performRequest(request, includeResponseStorage: includeResponseStorage, customDelegate: delegate)

0 commit comments

Comments
 (0)