diff --git a/Pod/Classes/DRDAPIManager.m b/Pod/Classes/DRDAPIManager.m index dc5ba4a..5c6cfa0 100644 --- a/Pod/Classes/DRDAPIManager.m +++ b/Pod/Classes/DRDAPIManager.m @@ -286,10 +286,6 @@ - (void)callAPICompletion:(DRDBaseAPI *)api #pragma mark - Send Batch Requests - (void)sendBatchAPIRequests:(nonnull DRDAPIBatchAPIRequests *)apis { NSParameterAssert(apis); - - NSAssert([[apis.apiRequestsSet valueForKeyPath:@"hash"] count] == [apis.apiRequestsSet count], - @"Should not have same API"); - dispatch_group_t batch_api_group = dispatch_group_create(); __weak typeof(self) weakSelf = self; [apis.apiRequestsSet enumerateObjectsUsingBlock:^(id api, BOOL * stop) { @@ -341,9 +337,9 @@ - (void)_sendSingleAPIRequest:(DRDBaseAPI *)api __weak typeof(self) weakSelf = self; NSString *requestUrlStr = [self requestUrlStringWithAPI:api]; id requestParams = [self requestParamsWithAPI:api]; - NSString *hashKey = [NSString stringWithFormat:@"%lu", (unsigned long)[api hash]]; + NSString *apiRequestKey = [api apiRequestKey]; - if ([self.sessionTasksCache objectForKey:hashKey]) { + if ([self.sessionTasksCache objectForKey:apiRequestKey]) { NSString *errorStr = self.configuration.frequentRequestErrorStr; NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : errorStr @@ -391,7 +387,7 @@ - (void)_sendSingleAPIRequest:(DRDBaseAPI *)api [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; } [strongSelf handleSuccWithResponse:responseObject andAPI:api]; - [strongSelf.sessionTasksCache removeObjectForKey:hashKey]; + [strongSelf.sessionTasksCache removeObjectForKey:apiRequestKey]; if (completionGroup) { dispatch_group_leave(completionGroup); } @@ -404,7 +400,7 @@ - (void)_sendSingleAPIRequest:(DRDBaseAPI *)api [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; } [strongSelf handleFailureWithError:error andAPI:api]; - [strongSelf.sessionTasksCache removeObjectForKey:hashKey]; + [strongSelf.sessionTasksCache removeObjectForKey:apiRequestKey]; if (completionGroup) { dispatch_group_leave(completionGroup); } @@ -507,7 +503,7 @@ - (void)_sendSingleAPIRequest:(DRDBaseAPI *)api break; } if (dataTask) { - [self.sessionTasksCache setObject:dataTask forKey:hashKey]; + [self.sessionTasksCache setObject:dataTask forKey:apiRequestKey]; } if ([[NSThread currentThread] isMainThread]) { @@ -521,9 +517,9 @@ - (void)_sendSingleAPIRequest:(DRDBaseAPI *)api - (void)cancelAPIRequest:(nonnull DRDBaseAPI *)api { dispatch_async(drd_api_task_creation_queue(), ^{ - NSString *hashKey = [NSString stringWithFormat:@"%lu", (unsigned long)[api hash]]; - NSURLSessionDataTask *dataTask = [self.sessionTasksCache objectForKey:hashKey]; - [self.sessionTasksCache removeObjectForKey:hashKey]; + NSString *apiRequestKey = [api apiRequestKey]; + NSURLSessionDataTask *dataTask = [self.sessionTasksCache objectForKey:apiRequestKey]; + [self.sessionTasksCache removeObjectForKey:apiRequestKey]; if (dataTask) { [dataTask cancel]; } diff --git a/Pod/Classes/DRDBaseAPI.h b/Pod/Classes/DRDBaseAPI.h index 26c096f..a228d24 100644 --- a/Pod/Classes/DRDBaseAPI.h +++ b/Pod/Classes/DRDBaseAPI.h @@ -235,6 +235,11 @@ NS_ASSUME_NONNULL_BEGIN */ - (nonnull DRDSecurityPolicy *)apiSecurityPolicy; +/** + * API存储在DataTaskCache中的Key + */ +- (nonnull NSString *)apiRequestKey; + #pragma mark - Process /** @@ -259,4 +264,4 @@ NS_ASSUME_NONNULL_BEGIN @end -NS_ASSUME_NONNULL_END \ No newline at end of file +NS_ASSUME_NONNULL_END diff --git a/Pod/Classes/DRDBaseAPI.m b/Pod/Classes/DRDBaseAPI.m index 679b64d..95d3e43 100644 --- a/Pod/Classes/DRDBaseAPI.m +++ b/Pod/Classes/DRDBaseAPI.m @@ -110,14 +110,16 @@ - (void)cancel { [[DRDAPIManager sharedDRDAPIManager] cancelAPIRequest:((DRDBaseAPI *)self)]; } -- (NSUInteger)hash { - NSMutableString *hashStr = [NSMutableString stringWithFormat:@"%@ %@ %@ %@", - [self requestMethod], [self requestParameters], [self baseUrl], [self customRequestUrl]]; - return [hashStr hash]; +- (NSString *)apiRequestKey { + return [NSString stringWithFormat:@"%@_%@_%@_%@", + [self requestMethod] ? : @"", + self.requestParameters ? : @"", + [self baseUrl] ? : @"", + [self customRequestUrl] ? : @""]; } -(BOOL)isEqualToAPI:(DRDBaseAPI *)api { - return [self hash] == [api hash]; + return [[self apiRequestKey] isEqualToString:[api apiRequestKey]]; } - (BOOL)isEqual:(id)object {