Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions Pod/Classes/DRDAPIManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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]) {
Expand All @@ -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];
}
Expand Down
7 changes: 6 additions & 1 deletion Pod/Classes/DRDBaseAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (nonnull DRDSecurityPolicy *)apiSecurityPolicy;

/**
* API存储在DataTaskCache中的Key
*/
- (nonnull NSString *)apiRequestKey;

#pragma mark - Process

/**
Expand All @@ -259,4 +264,4 @@ NS_ASSUME_NONNULL_BEGIN

@end

NS_ASSUME_NONNULL_END
NS_ASSUME_NONNULL_END
12 changes: 7 additions & 5 deletions Pod/Classes/DRDBaseAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down