@@ -54,7 +54,9 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
54
54
totalBytesSent : (int64_t )totalBytesSent
55
55
totalBytesExpectedToSend : (int64_t )totalBytesExpectedToSend {
56
56
57
- _progressBlock (totalBytesSent, totalBytesExpectedToSend);
57
+ if (_progressBlock) {
58
+ _progressBlock (totalBytesSent, totalBytesExpectedToSend);
59
+ }
58
60
if (_cancelBlock && _cancelBlock ()) {
59
61
[_task cancel ];
60
62
}
@@ -63,11 +65,12 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
63
65
@end
64
66
65
67
@interface QNSessionManager ()
66
- @property (nonatomic ) NSURLSession *httpManager;
67
68
@property UInt32 timeout;
68
69
@property (nonatomic , strong ) QNUrlConvert converter;
69
70
@property bool noProxy;
71
+ @property (nonatomic ,strong ) NSDictionary *proxyDict;
70
72
@property (nonatomic ) QNDnsManager *dns;
73
+ @property (nonatomic ,strong ) NSOperationQueue *delegateQueue;
71
74
@end
72
75
73
76
@implementation QNSessionManager
@@ -79,13 +82,11 @@ - (instancetype)initWithProxy:(NSDictionary *)proxyDict
79
82
if (self = [super init ]) {
80
83
if (proxyDict != nil ) {
81
84
_noProxy = NO ;
85
+ _proxyDict = proxyDict;
82
86
} else {
83
87
_noProxy = YES ;
84
88
}
85
- NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration ];
86
- configuration.connectionProxyDictionary = proxyDict;
87
- _httpManager = [NSURLSession sessionWithConfiguration: configuration delegate: [[QNProgessDelegate alloc ] initWithProgress: nil ] delegateQueue: [NSOperationQueue currentQueue ]];
88
-
89
+ _delegateQueue = [[NSOperationQueue alloc ] init ];
89
90
_timeout = timeout;
90
91
_converter = converter;
91
92
_dns = dns;
@@ -94,12 +95,6 @@ - (instancetype)initWithProxy:(NSDictionary *)proxyDict
94
95
return self;
95
96
}
96
97
97
- - (void )dealloc {
98
- if (self.httpManager ) {
99
- [self .httpManager finishTasksAndInvalidate ];
100
- }
101
- }
102
-
103
98
- (instancetype )init {
104
99
return [self initWithProxy: nil timeout: 60 urlConverter: nil dns: nil ];
105
100
}
@@ -196,9 +191,14 @@ - (void)sendRequest2:(NSMutableURLRequest *)request
196
191
QNInternalProgressBlock progressBlock2 = ^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {
197
192
progressBlock (totalBytesWritten, totalBytesExpectedToWrite);
198
193
};
199
- __block QNProgessDelegate *delegate = ( QNProgessDelegate *)_httpManager. delegate ;
194
+ __block QNProgessDelegate *delegate = [[ QNProgessDelegate alloc ] initWithProgress: nil ] ;
200
195
delegate.progressBlock = progressBlock2;
201
- NSURLSessionUploadTask *uploadTask = [_httpManager uploadTaskWithRequest: request fromData: nil completionHandler: ^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
196
+ NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration ];
197
+ if (_proxyDict) {
198
+ configuration.connectionProxyDictionary = _proxyDict;
199
+ }
200
+ __block NSURLSession *session = [NSURLSession sessionWithConfiguration: configuration delegate: delegate delegateQueue: _delegateQueue];
201
+ NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest: request fromData: nil completionHandler: ^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
202
202
203
203
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
204
204
double duration = [[NSDate date ] timeIntervalSinceDate: startTime];
@@ -221,6 +221,7 @@ - (void)sendRequest2:(NSMutableURLRequest *)request
221
221
delegate.cancelBlock = nil ;
222
222
delegate.progressBlock = nil ;
223
223
completeBlock (info, resp);
224
+ [session finishTasksAndInvalidate ];
224
225
}];
225
226
delegate.task = uploadTask;
226
227
delegate.cancelBlock = cancelBlock;
0 commit comments