mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-11-25 01:22:41 +00:00
Refactor sender methods to enable testing of async code
This commit is contained in:
parent
e32c1e5c8b
commit
2bcd14d132
@ -36,12 +36,18 @@ static NSUInteger const defaultRequestLimit = 10;
|
||||
queue:nil
|
||||
usingBlock:^(NSNotification *notification) {
|
||||
typeof(self) strongSelf = weakSelf;
|
||||
[strongSelf sendSavedData];
|
||||
[strongSelf sendSavedDataAsync];
|
||||
}];
|
||||
}
|
||||
|
||||
#pragma mark - Sending
|
||||
|
||||
- (void)sendSavedDataAsync{
|
||||
dispatch_async(self.senderQueue, ^{
|
||||
[self sendSavedData];
|
||||
});
|
||||
}
|
||||
|
||||
- (void)sendSavedData{
|
||||
@synchronized(self){
|
||||
if(_runningRequestsCount < _maxRequestCount){
|
||||
@ -50,13 +56,9 @@ static NSUInteger const defaultRequestLimit = 10;
|
||||
return;
|
||||
}
|
||||
}
|
||||
__weak typeof(self) weakSelf = self;
|
||||
dispatch_async(self.senderQueue, ^{
|
||||
typeof(self) strongSelf = weakSelf;
|
||||
NSString *path = [self.persistence requestNextPath];
|
||||
NSData *data = [self.persistence dataAtPath:path];
|
||||
[strongSelf sendData:data withPath:path];
|
||||
});
|
||||
NSString *path = [self.persistence requestNextPath];
|
||||
NSData *data = [self.persistence dataAtPath:path];
|
||||
[self sendData:data withPath:path];
|
||||
}
|
||||
|
||||
- (void)sendData:(NSData * __nonnull)data withPath:(NSString * __nonnull)path {
|
||||
@ -87,7 +89,7 @@ static NSUInteger const defaultRequestLimit = 10;
|
||||
NSInteger statusCode = httpResponse.statusCode;
|
||||
[strongSelf handleResponseWithStatusCode:statusCode responseData:data filePath:path error:error];
|
||||
}];
|
||||
[task resume];
|
||||
[self resumeSessionDataTask:task];
|
||||
}else{
|
||||
BITHTTPOperation *operation = [BITHTTPOperation operationWithRequest:request];
|
||||
[operation setCompletion:^(BITHTTPOperation *operation, NSData *responseData, NSError *error) {
|
||||
@ -100,6 +102,10 @@ static NSUInteger const defaultRequestLimit = 10;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)resumeSessionDataTask:(NSURLSessionDataTask *)sessionDataTask {
|
||||
[sessionDataTask resume];
|
||||
}
|
||||
|
||||
- (void)handleResponseWithStatusCode:(NSInteger)statusCode responseData:(NSData *)responseData filePath:(NSString *)filePath error:(NSError *)error{
|
||||
self.runningRequestsCount -= 1;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user