From 16ec2282a30e237d9cbfe6d26af0afe8753e79a9 Mon Sep 17 00:00:00 2001 From: zhouzhongguang Date: Wed, 29 Jul 2020 11:18:52 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=BC=80=E6=94=BEloadDataWithParams?= =?UTF-8?q?=E5=AE=9E=E4=BE=8B=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.h | 1 + 1 file changed, 1 insertion(+) diff --git a/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.h b/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.h index 7e6ffb2..a06c835 100644 --- a/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.h +++ b/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.h @@ -36,6 +36,7 @@ // start - (NSInteger)loadData; +- (NSInteger)loadDataWithParams:(NSDictionary * _Nullable)params success:(void (^ _Nullable)(CTAPIBaseManager * _Nonnull apiManager))successCallback fail:(void (^ _Nullable)(CTAPIBaseManager * _Nonnull apiManager))failCallback; + (NSInteger)loadDataWithParams:(NSDictionary * _Nullable)params success:(void (^ _Nullable)(CTAPIBaseManager * _Nonnull apiManager))successCallback fail:(void (^ _Nullable)(CTAPIBaseManager * _Nonnull apiManager))failCallback; // cancel From bf79efbe6e2e9c976b1be88ffac92cfd75aa1c49 Mon Sep 17 00:00:00 2001 From: zhouzhongguang <1137138434@qq.com> Date: Thu, 16 Jun 2022 10:22:43 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9Aservice=E6=83=85?= =?UTF-8?q?=E5=86=B5=E4=B8=8BdispatchTable=E6=93=8D=E4=BD=9C=E5=81=B6?= =?UTF-8?q?=E5=B0=94=E5=B4=A9=E6=BA=83=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit requestId 类型改为 NSString,requestId 的值由 service 类名和taskIdentifier拼装而成 --- .../CTNetworking/CTNetworkingDefines.h | 2 +- .../Components/APIProxy/CTApiProxy.h | 4 +-- .../Components/APIProxy/CTApiProxy.m | 24 ++++++++++------ .../BaseAPIManager/CTAPIBaseManager.h | 8 +++--- .../BaseAPIManager/CTAPIBaseManager.m | 28 +++++++++---------- .../Components/URLResponse/CTURLResponse.h | 4 +-- .../Components/URLResponse/CTURLResponse.m | 8 +++--- 7 files changed, 42 insertions(+), 36 deletions(-) diff --git a/CTNetworking/CTNetworking/CTNetworkingDefines.h b/CTNetworking/CTNetworking/CTNetworkingDefines.h index d865cd5..16337b3 100644 --- a/CTNetworking/CTNetworking/CTNetworkingDefines.h +++ b/CTNetworking/CTNetworking/CTNetworkingDefines.h @@ -71,7 +71,7 @@ extern NSString * _Nonnull const kCTApiProxyValidateResultKeyResponseString; @optional - (void)cleanData; - (NSDictionary *_Nullable)reformParams:(NSDictionary *_Nullable)params; -- (NSInteger)loadDataWithParams:(NSDictionary *_Nullable)params; +- (NSString *_Nonnull)loadDataWithParams:(NSDictionary *_Nullable)params; @end diff --git a/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.h b/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.h index 5a1b41f..80e9ffe 100644 --- a/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.h +++ b/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.h @@ -16,8 +16,8 @@ typedef void(^CTCallback)(CTURLResponse *response); + (instancetype)sharedInstance; -- (NSNumber *)callApiWithRequest:(NSURLRequest *)request success:(CTCallback)success fail:(CTCallback)fail; -- (void)cancelRequestWithRequestID:(NSNumber *)requestID; +- (NSString *)callApiWithRequest:(NSURLRequest *)request success:(CTCallback)success fail:(CTCallback)fail; +- (void)cancelRequestWithRequestID:(NSString *)requestID; - (void)cancelRequestWithRequestIDList:(NSArray *)requestIDList; @end diff --git a/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m b/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m index 31bc155..54172eb 100755 --- a/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m +++ b/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m @@ -25,7 +25,7 @@ @interface CTApiProxy () @property (nonatomic, strong) NSMutableDictionary *dispatchTable; -@property (nonatomic, strong) NSNumber *recordedRequestId; +//@property (nonatomic, strong) NSNumber *recordedRequestId; @end @@ -51,6 +51,12 @@ - (AFHTTPSessionManager *)sessionManagerWithService:(id)servi return sessionManager; } +- (NSString *)requestIDWithService:(id)service dataTask:(NSURLSessionDataTask *)dataTask { + NSString *requestId = [NSString stringWithFormat:@"%@-%ld", NSStringFromClass(service.class), [dataTask taskIdentifier]]; + + return requestId; +} + #pragma mark - life cycle + (instancetype)sharedInstance { @@ -63,7 +69,7 @@ + (instancetype)sharedInstance } #pragma mark - public methods -- (void)cancelRequestWithRequestID:(NSNumber *)requestID +- (void)cancelRequestWithRequestID:(NSString *)requestID { NSURLSessionDataTask *requestOperation = self.dispatchTable[requestID]; [requestOperation cancel]; @@ -72,21 +78,21 @@ - (void)cancelRequestWithRequestID:(NSNumber *)requestID - (void)cancelRequestWithRequestIDList:(NSArray *)requestIDList { - for (NSNumber *requestId in requestIDList) { + for (NSString *requestId in requestIDList) { [self cancelRequestWithRequestID:requestId]; } } /** 这个函数存在的意义在于,如果将来要把AFNetworking换掉,只要修改这个函数的实现即可。 */ -- (NSNumber *)callApiWithRequest:(NSURLRequest *)request success:(CTCallback)success fail:(CTCallback)fail +- (NSString *)callApiWithRequest:(NSURLRequest *)request success:(CTCallback)success fail:(CTCallback)fail { // 跑到这里的block的时候,就已经是主线程了。 __block NSURLSessionDataTask *dataTask = nil; dataTask = [[self sessionManagerWithService:request.service] dataTaskWithRequest:request - uploadProgress:nil - downloadProgress:nil - completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { - NSNumber *requestID = @([dataTask taskIdentifier]); + uploadProgress:nil + downloadProgress:nil + completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { + NSString *requestID = [self requestIDWithService:request.service dataTask:dataTask]; [self.dispatchTable removeObjectForKey:requestID]; NSDictionary *result = [request.service resultWithResponseObject:responseObject response:response request:request error:&error]; @@ -110,7 +116,7 @@ - (NSNumber *)callApiWithRequest:(NSURLRequest *)request success:(CTCallback)suc } }]; - NSNumber *requestId = @([dataTask taskIdentifier]); + NSString *requestId = [self requestIDWithService:request.service dataTask:dataTask]; self.dispatchTable[requestId] = dataTask; [dataTask resume]; diff --git a/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.h b/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.h index a06c835..c2bc7fd 100644 --- a/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.h +++ b/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.h @@ -35,13 +35,13 @@ @property (nonatomic, assign, readonly) BOOL isLoading; // start -- (NSInteger)loadData; -- (NSInteger)loadDataWithParams:(NSDictionary * _Nullable)params success:(void (^ _Nullable)(CTAPIBaseManager * _Nonnull apiManager))successCallback fail:(void (^ _Nullable)(CTAPIBaseManager * _Nonnull apiManager))failCallback; -+ (NSInteger)loadDataWithParams:(NSDictionary * _Nullable)params success:(void (^ _Nullable)(CTAPIBaseManager * _Nonnull apiManager))successCallback fail:(void (^ _Nullable)(CTAPIBaseManager * _Nonnull apiManager))failCallback; +- (NSString *_Nonnull)loadData; +- (NSString *_Nonnull)loadDataWithParams:(NSDictionary * _Nullable)params success:(void (^ _Nullable)(CTAPIBaseManager * _Nonnull apiManager))successCallback fail:(void (^ _Nullable)(CTAPIBaseManager * _Nonnull apiManager))failCallback; ++ (NSString *_Nonnull)loadDataWithParams:(NSDictionary * _Nullable)params success:(void (^ _Nullable)(CTAPIBaseManager * _Nonnull apiManager))successCallback fail:(void (^ _Nullable)(CTAPIBaseManager * _Nonnull apiManager))failCallback; // cancel - (void)cancelAllRequests; -- (void)cancelRequestWithRequestId:(NSInteger)requestID; +- (void)cancelRequestWithRequestId:(NSString *_Nonnull)requestID; // finish - (id _Nullable )fetchDataWithReformer:(id _Nullable)reformer; diff --git a/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m b/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m index ffe6161..432d381 100644 --- a/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m +++ b/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m @@ -84,10 +84,10 @@ - (void)cancelAllRequests [self.requestIdList removeAllObjects]; } -- (void)cancelRequestWithRequestId:(NSInteger)requestID +- (void)cancelRequestWithRequestId:(NSString *)requestID { [self removeRequestIdWithRequestID:requestID]; - [[CTApiProxy sharedInstance] cancelRequestWithRequestID:@(requestID)]; + [[CTApiProxy sharedInstance] cancelRequestWithRequestID:requestID]; } - (id)fetchDataWithReformer:(id)reformer @@ -102,19 +102,19 @@ - (id)fetchDataWithReformer:(id)reformer } #pragma mark - calling api -- (NSInteger)loadData +- (NSString *)loadData { NSDictionary *params = [self.paramSource paramsForApi:self]; - NSInteger requestId = [self loadDataWithParams:params]; + NSString *requestId = [self loadDataWithParams:params]; return requestId; } -+ (NSInteger)loadDataWithParams:(NSDictionary *)params success:(void (^)(CTAPIBaseManager *))successCallback fail:(void (^)(CTAPIBaseManager *))failCallback ++ (NSString *)loadDataWithParams:(NSDictionary *)params success:(void (^)(CTAPIBaseManager *))successCallback fail:(void (^)(CTAPIBaseManager *))failCallback { return [[[self alloc] init] loadDataWithParams:params success:successCallback fail:failCallback]; } -- (NSInteger)loadDataWithParams:(NSDictionary *)params success:(void (^)(CTAPIBaseManager *))successCallback fail:(void (^)(CTAPIBaseManager *))failCallback +- (NSString *)loadDataWithParams:(NSDictionary *)params success:(void (^)(CTAPIBaseManager *))successCallback fail:(void (^)(CTAPIBaseManager *))failCallback { self.successBlock = successCallback; self.failBlock = failCallback; @@ -122,9 +122,9 @@ - (NSInteger)loadDataWithParams:(NSDictionary *)params success:(void (^)(CTAPIBa return [self loadDataWithParams:params]; } -- (NSInteger)loadDataWithParams:(NSDictionary *)params +- (NSString *)loadDataWithParams:(NSDictionary *)params { - NSInteger requestId = 0; + NSString *requestId = @""; NSDictionary *reformedParams = [self reformParams:params]; if (reformedParams == nil) { reformedParams = @{}; @@ -158,7 +158,7 @@ - (NSInteger)loadDataWithParams:(NSDictionary *)params request.service = service; [CTLogger logDebugInfoWithRequest:request apiName:self.child.methodName service:service]; - NSNumber *requestId = [[CTApiProxy sharedInstance] callApiWithRequest:request success:^(CTURLResponse *response) { + NSString *requestId = [[CTApiProxy sharedInstance] callApiWithRequest:request success:^(CTURLResponse *response) { [self successedOnCallingAPI:response]; } fail:^(CTURLResponse *response) { CTAPIManagerErrorType errorType = CTAPIManagerErrorTypeDefault; @@ -178,7 +178,7 @@ - (NSInteger)loadDataWithParams:(NSDictionary *)params NSMutableDictionary *params = [reformedParams mutableCopy]; params[kCTAPIBaseManagerRequestID] = requestId; [self afterCallingAPIWithParams:params]; - return [requestId integerValue]; + return requestId; } else { [self failedOnCallingAPI:nil withErrorType:CTAPIManagerErrorTypeNoNetWork]; @@ -405,11 +405,11 @@ - (NSDictionary *)reformParams:(NSDictionary *)params } #pragma mark - private methods -- (void)removeRequestIdWithRequestID:(NSInteger)requestId +- (void)removeRequestIdWithRequestID:(NSString *)requestId { - NSNumber *requestIDToRemove = nil; - for (NSNumber *storedRequestId in self.requestIdList) { - if ([storedRequestId integerValue] == requestId) { + NSString *requestIDToRemove = nil; + for (NSString *storedRequestId in self.requestIdList) { + if ([storedRequestId isEqualToString:requestId]) { requestIDToRemove = storedRequestId; } } diff --git a/CTNetworking/CTNetworking/Components/URLResponse/CTURLResponse.h b/CTNetworking/CTNetworking/Components/URLResponse/CTURLResponse.h index f5e1986..01450eb 100644 --- a/CTNetworking/CTNetworking/Components/URLResponse/CTURLResponse.h +++ b/CTNetworking/CTNetworking/Components/URLResponse/CTURLResponse.h @@ -21,7 +21,7 @@ typedef NS_ENUM(NSUInteger, CTURLResponseStatus) @property (nonatomic, assign, readonly) CTURLResponseStatus status; @property (nonatomic, copy, readonly) NSString *contentString; @property (nonatomic, copy, readonly) id content; -@property (nonatomic, assign, readonly) NSInteger requestId; +@property (nonatomic, copy, readonly) NSString *requestId; @property (nonatomic, copy, readonly) NSURLRequest *request; @property (nonatomic, copy, readonly) NSData *responseData; @property (nonatomic, strong, readonly) NSString *errorMessage; @@ -32,7 +32,7 @@ typedef NS_ENUM(NSUInteger, CTURLResponseStatus) @property (nonatomic, assign, readonly) BOOL isCache; -- (instancetype)initWithResponseString:(NSString *)responseString requestId:(NSNumber *)requestId request:(NSURLRequest *)request responseObject:(id)responseObject error:(NSError *)error; +- (instancetype)initWithResponseString:(NSString *)responseString requestId:(NSString *)requestId request:(NSURLRequest *)request responseObject:(id)responseObject error:(NSError *)error; // 使用initWithData的response,它的isCache是YES,上面两个函数生成的response的isCache是NO - (instancetype)initWithData:(NSData *)data; diff --git a/CTNetworking/CTNetworking/Components/URLResponse/CTURLResponse.m b/CTNetworking/CTNetworking/Components/URLResponse/CTURLResponse.m index 06fecc3..0bc9b47 100644 --- a/CTNetworking/CTNetworking/Components/URLResponse/CTURLResponse.m +++ b/CTNetworking/CTNetworking/Components/URLResponse/CTURLResponse.m @@ -16,7 +16,7 @@ @interface CTURLResponse () @property (nonatomic, copy, readwrite) NSString *contentString; @property (nonatomic, copy, readwrite) id content; @property (nonatomic, copy, readwrite) NSURLRequest *request; -@property (nonatomic, assign, readwrite) NSInteger requestId; +@property (nonatomic, copy, readwrite) NSString *requestId; @property (nonatomic, copy, readwrite) NSData *responseData; @property (nonatomic, assign, readwrite) BOOL isCache; @property (nonatomic, strong, readwrite) NSString *errorMessage; @@ -26,12 +26,12 @@ @interface CTURLResponse () @implementation CTURLResponse #pragma mark - life cycle -- (instancetype)initWithResponseString:(NSString *)responseString requestId:(NSNumber *)requestId request:(NSURLRequest *)request responseObject:(NSDictionary *)responseObject error:(NSError *)error +- (instancetype)initWithResponseString:(NSString *)responseString requestId:(NSString *)requestId request:(NSURLRequest *)request responseObject:(NSDictionary *)responseObject error:(NSError *)error { self = [super init]; if (self) { self.contentString = [responseString CT_defaultValue:@""]; - self.requestId = [requestId integerValue]; + self.requestId = requestId; self.request = request; self.acturlRequestParams = request.actualRequestParams; self.originRequestParams = request.originRequestParams; @@ -49,7 +49,7 @@ - (instancetype)initWithData:(NSData *)data if (self) { self.contentString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; self.status = [self responseStatusWithError:nil]; - self.requestId = 0; + self.requestId = @""; self.request = nil; self.responseData = data; self.content = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL]; From a5707b542c44f6019863d218a4fe82fd28386d27 Mon Sep 17 00:00:00 2001 From: zhouzhongguang <1137138434@qq.com> Date: Fri, 5 Aug 2022 10:57:47 +0800 Subject: [PATCH 3/8] =?UTF-8?q?CTApiProxy=20=E4=B8=AD=E7=9A=84=20dispatchT?= =?UTF-8?q?able=20=E5=A2=9E=E5=8A=A0=E8=AF=BB=E5=86=99=E9=94=81=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B9=B6=E5=8F=91=E8=AE=BF=E9=97=AE=E6=97=B6?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E7=9A=84=E5=B4=A9=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/APIProxy/CTApiProxy.m | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m b/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m index 54172eb..5189292 100755 --- a/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m +++ b/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m @@ -14,6 +14,7 @@ #import "NSString+AXNetworkingMethods.h" #import "NSObject+AXNetworkingMethods.h" #import "CTMediator+CTAppContext.h" +#import static NSString * const kAXApiProxyDispatchItemKeyCallbackSuccess = @"kAXApiProxyDispatchItemCallbackSuccess"; static NSString * const kAXApiProxyDispatchItemKeyCallbackFail = @"kAXApiProxyDispatchItemCallbackFail"; @@ -23,6 +24,9 @@ //NSString * const kCTApiProxyValidateResultKeyResponseData = @"kCTApiProxyValidateResultKeyResponseData"; @interface CTApiProxy () +{ + pthread_rwlock_t _dispatchTableLock; +} @property (nonatomic, strong) NSMutableDictionary *dispatchTable; //@property (nonatomic, strong) NSNumber *recordedRequestId; @@ -30,6 +34,16 @@ @interface CTApiProxy () @end @implementation CTApiProxy + +- (instancetype)init +{ + self = [super init]; + if (self) { + pthread_rwlock_init(&_dispatchTableLock, NULL); + } + return self; +} + #pragma mark - getters and setters - (NSMutableDictionary *)dispatchTable { @@ -71,9 +85,11 @@ + (instancetype)sharedInstance #pragma mark - public methods - (void)cancelRequestWithRequestID:(NSString *)requestID { + pthread_rwlock_wrlock(&_dispatchTableLock); NSURLSessionDataTask *requestOperation = self.dispatchTable[requestID]; [requestOperation cancel]; [self.dispatchTable removeObjectForKey:requestID]; + pthread_rwlock_unlock(&_dispatchTableLock); } - (void)cancelRequestWithRequestIDList:(NSArray *)requestIDList @@ -93,7 +109,9 @@ - (NSString *)callApiWithRequest:(NSURLRequest *)request success:(CTCallback)suc downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { NSString *requestID = [self requestIDWithService:request.service dataTask:dataTask]; + pthread_rwlock_wrlock(&_dispatchTableLock); [self.dispatchTable removeObjectForKey:requestID]; + pthread_rwlock_unlock(&_dispatchTableLock); NSDictionary *result = [request.service resultWithResponseObject:responseObject response:response request:request error:&error]; // 输出返回数据 @@ -118,7 +136,10 @@ - (NSString *)callApiWithRequest:(NSURLRequest *)request success:(CTCallback)suc NSString *requestId = [self requestIDWithService:request.service dataTask:dataTask]; + pthread_rwlock_wrlock(&_dispatchTableLock); self.dispatchTable[requestId] = dataTask; + pthread_rwlock_unlock(&_dispatchTableLock); + [dataTask resume]; return requestId; From b5ebf491336f9059ff602d301b81b40a612a7f34 Mon Sep 17 00:00:00 2001 From: zhouzhongguang <1137138434@qq.com> Date: Thu, 11 Aug 2022 10:43:53 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20CTServiceFactory=20?= =?UTF-8?q?=E5=8D=95=E4=BE=8B=E4=B8=AD=20serviceStorage=20=E7=9A=84?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E8=AE=BF=E9=97=AE=E5=8F=AF=E8=83=BD=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84=E5=B4=A9=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CTNetworking/Services/CTServiceFactory.m | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/CTNetworking/CTNetworking/Services/CTServiceFactory.m b/CTNetworking/CTNetworking/Services/CTServiceFactory.m index 8c17f33..5d25004 100644 --- a/CTNetworking/CTNetworking/Services/CTServiceFactory.m +++ b/CTNetworking/CTNetworking/Services/CTServiceFactory.m @@ -20,13 +20,13 @@ @interface CTServiceFactory () @implementation CTServiceFactory #pragma mark - getters and setters -- (NSMutableDictionary *)serviceStorage -{ - if (_serviceStorage == nil) { - _serviceStorage = [[NSMutableDictionary alloc] init]; - } - return _serviceStorage; -} +//- (NSMutableDictionary *)serviceStorage +//{ +// if (_serviceStorage == nil) { +// _serviceStorage = [[NSMutableDictionary alloc] init]; +// } +// return _serviceStorage; +//} #pragma mark - life cycle + (instancetype)sharedInstance @@ -39,12 +39,25 @@ + (instancetype)sharedInstance return sharedInstance; } +- (instancetype)init { + self = [super init]; + if (self) { + // 防止出现并发问题,不再使用懒加载方式初始化 + _serviceStorage = [[NSMutableDictionary alloc] init]; + } + + return self; +} + #pragma mark - public methods - (id )serviceWithIdentifier:(NSString *)identifier { - if (self.serviceStorage[identifier] == nil) { - self.serviceStorage[identifier] = [self newServiceWithIdentifier:identifier]; + @synchronized (self.serviceStorage) { + if (self.serviceStorage[identifier] == nil) { + self.serviceStorage[identifier] = [self newServiceWithIdentifier:identifier]; + } } + return self.serviceStorage[identifier]; } From 26b0df6ccf5b74f4709c6d6bfc36bd3060f6ba8f Mon Sep 17 00:00:00 2001 From: zhouzhongguang <1137138434@qq.com> Date: Tue, 30 Aug 2022 11:50:15 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0cancelAllRequests?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加cancelAllRequests方法,用于取消所有未结束的请求 --- .../CTNetworking/Components/APIProxy/CTApiProxy.h | 2 ++ .../CTNetworking/Components/APIProxy/CTApiProxy.m | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.h b/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.h index 80e9ffe..4654534 100644 --- a/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.h +++ b/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.h @@ -20,4 +20,6 @@ typedef void(^CTCallback)(CTURLResponse *response); - (void)cancelRequestWithRequestID:(NSString *)requestID; - (void)cancelRequestWithRequestIDList:(NSArray *)requestIDList; +- (void)cancelAllRequests; + @end diff --git a/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m b/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m index 5189292..0fe0e2c 100755 --- a/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m +++ b/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m @@ -83,6 +83,21 @@ + (instancetype)sharedInstance } #pragma mark - public methods + +- (void)cancelAllRequests +{ + pthread_rwlock_wrlock(&_dispatchTableLock); + + [self.dispatchTable enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, + NSURLSessionDataTask * _Nonnull obj, + BOOL * _Nonnull stop) { + [obj cancel]; + }]; + [self.dispatchTable removeAllObjects]; + + pthread_rwlock_unlock(&_dispatchTableLock); +} + - (void)cancelRequestWithRequestID:(NSString *)requestID { pthread_rwlock_wrlock(&_dispatchTableLock); From d56fad5542df0663bd319a03efbc85473a5250ab Mon Sep 17 00:00:00 2001 From: zhouzhongguang Date: Thu, 27 Oct 2022 16:26:46 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复类型不对进行相等性比较导致的崩溃 --- .../CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m b/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m index 432d381..074d9dc 100644 --- a/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m +++ b/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m @@ -407,6 +407,10 @@ - (NSDictionary *)reformParams:(NSDictionary *)params #pragma mark - private methods - (void)removeRequestIdWithRequestID:(NSString *)requestId { + if (![requestId isKindOfClass:[NSString class]]) { + return; + } + NSString *requestIDToRemove = nil; for (NSString *storedRequestId in self.requestIdList) { if ([storedRequestId isEqualToString:requestId]) { From 423b73a6950c5e6fc325ce060e3e5887514e3239 Mon Sep 17 00:00:00 2001 From: zhouzhongguang Date: Fri, 28 Oct 2022 10:06:37 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E5=8A=A0=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CTAPIBaseManager中requestIdList的写访问加锁 --- CTNetworking.xcodeproj/project.pbxproj | 2 +- .../BaseAPIManager/CTAPIBaseManager.m | 24 +++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/CTNetworking.xcodeproj/project.pbxproj b/CTNetworking.xcodeproj/project.pbxproj index 067a848..49c6318 100644 --- a/CTNetworking.xcodeproj/project.pbxproj +++ b/CTNetworking.xcodeproj/project.pbxproj @@ -239,8 +239,8 @@ 4A1E17A02045585A00BDBF7B /* BaseAPIManager */ = { isa = PBXGroup; children = ( - 4A1E17A12045585A00BDBF7B /* CTAPIBaseManager.m */, 4A1E17A22045585A00BDBF7B /* CTAPIBaseManager.h */, + 4A1E17A12045585A00BDBF7B /* CTAPIBaseManager.m */, ); path = BaseAPIManager; sourceTree = ""; diff --git a/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m b/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m index 074d9dc..467c708 100644 --- a/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m +++ b/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m @@ -15,6 +15,9 @@ #import "CTMediator+CTAppContext.h" #import "CTServiceFactory.h" +#import + + NSString * const kCTUserTokenInvalidNotification = @"kCTUserTokenInvalidNotification"; NSString * const kCTUserTokenIllegalNotification = @"kCTUserTokenIllegalNotification"; @@ -23,6 +26,9 @@ @interface CTAPIBaseManager () +{ + pthread_rwlock_t _requestIdListLock; +} @property (nonatomic, strong, readwrite) id fetchedRawData; @property (nonatomic, assign, readwrite) BOOL isLoading; @@ -55,6 +61,8 @@ - (instancetype)init _memoryCacheSecond = 3 * 60; _diskCacheSecond = 3 * 60; + pthread_rwlock_init(&_requestIdListLock, NULL); + if ([self conformsToProtocol:@protocol(CTAPIManager)]) { self.child = (id )self; } else { @@ -68,7 +76,7 @@ - (instancetype)init - (void)dealloc { [self cancelAllRequests]; - self.requestIdList = nil; + _requestIdList = nil; } #pragma mark - NSCopying @@ -81,7 +89,10 @@ - (id)copyWithZone:(NSZone *)zone - (void)cancelAllRequests { [[CTApiProxy sharedInstance] cancelRequestWithRequestIDList:self.requestIdList]; + + pthread_rwlock_wrlock(&_requestIdListLock); [self.requestIdList removeAllObjects]; + pthread_rwlock_unlock(&_requestIdListLock); } - (void)cancelRequestWithRequestId:(NSString *)requestID @@ -173,7 +184,10 @@ - (NSString *)loadDataWithParams:(NSDictionary *)params } [self failedOnCallingAPI:response withErrorType:errorType]; }]; + + pthread_rwlock_wrlock(&_requestIdListLock); [self.requestIdList addObject:requestId]; + pthread_rwlock_unlock(&_requestIdListLock); NSMutableDictionary *params = [reformedParams mutableCopy]; params[kCTAPIBaseManagerRequestID] = requestId; @@ -407,10 +421,8 @@ - (NSDictionary *)reformParams:(NSDictionary *)params #pragma mark - private methods - (void)removeRequestIdWithRequestID:(NSString *)requestId { - if (![requestId isKindOfClass:[NSString class]]) { - return; - } - + pthread_rwlock_wrlock(&_requestIdListLock); + NSString *requestIDToRemove = nil; for (NSString *storedRequestId in self.requestIdList) { if ([storedRequestId isEqualToString:requestId]) { @@ -420,6 +432,8 @@ - (void)removeRequestIdWithRequestID:(NSString *)requestId if (requestIDToRemove) { [self.requestIdList removeObject:requestIDToRemove]; } + + pthread_rwlock_unlock(&_requestIdListLock); } #pragma mark - getters and setters From 79574a32de782b805e4ffc9b4490c99081a57c42 Mon Sep 17 00:00:00 2001 From: zhouzhongguang Date: Tue, 17 Oct 2023 11:24:50 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=94=81=E7=9A=84?= =?UTF-8?q?=E9=94=80=E6=AF=81=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pthread_rwlock_t 锁,在dealloc方法中增加销毁的代码 --- CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m | 4 ++++ .../CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m | 2 ++ 2 files changed, 6 insertions(+) diff --git a/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m b/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m index 0fe0e2c..c329c43 100755 --- a/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m +++ b/CTNetworking/CTNetworking/Components/APIProxy/CTApiProxy.m @@ -44,6 +44,10 @@ - (instancetype)init return self; } +- (void)dealloc { + pthread_rwlock_destroy(&_dispatchTableLock); +} + #pragma mark - getters and setters - (NSMutableDictionary *)dispatchTable { diff --git a/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m b/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m index 467c708..5f70193 100644 --- a/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m +++ b/CTNetworking/CTNetworking/Components/BaseAPIManager/CTAPIBaseManager.m @@ -77,6 +77,8 @@ - (void)dealloc { [self cancelAllRequests]; _requestIdList = nil; + + pthread_rwlock_destroy(&_requestIdListLock); } #pragma mark - NSCopying