Skip to content

Commit ea4a335

Browse files
authored
Merge pull request #267 from wangliangliang2/master
fix in block bad access
2 parents 6b5d4c4 + 918ac57 commit ea4a335

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

QiniuSDK/Http/QNSessionManager.m

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
5454
totalBytesSent:(int64_t)totalBytesSent
5555
totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
5656

57-
_progressBlock(totalBytesSent, totalBytesExpectedToSend);
57+
if (_progressBlock) {
58+
_progressBlock(totalBytesSent, totalBytesExpectedToSend);
59+
}
5860
if (_cancelBlock && _cancelBlock()) {
5961
[_task cancel];
6062
}
@@ -63,11 +65,12 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
6365
@end
6466

6567
@interface QNSessionManager ()
66-
@property (nonatomic) NSURLSession *httpManager;
6768
@property UInt32 timeout;
6869
@property (nonatomic, strong) QNUrlConvert converter;
6970
@property bool noProxy;
71+
@property (nonatomic,strong) NSDictionary *proxyDict;
7072
@property (nonatomic) QNDnsManager *dns;
73+
@property (nonatomic,strong) NSOperationQueue *delegateQueue;
7174
@end
7275

7376
@implementation QNSessionManager
@@ -79,13 +82,11 @@ - (instancetype)initWithProxy:(NSDictionary *)proxyDict
7982
if (self = [super init]) {
8083
if (proxyDict != nil) {
8184
_noProxy = NO;
85+
_proxyDict = proxyDict;
8286
} else {
8387
_noProxy = YES;
8488
}
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];
8990
_timeout = timeout;
9091
_converter = converter;
9192
_dns = dns;
@@ -94,12 +95,6 @@ - (instancetype)initWithProxy:(NSDictionary *)proxyDict
9495
return self;
9596
}
9697

97-
- (void)dealloc {
98-
if (self.httpManager) {
99-
[self.httpManager finishTasksAndInvalidate];
100-
}
101-
}
102-
10398
- (instancetype)init {
10499
return [self initWithProxy:nil timeout:60 urlConverter:nil dns:nil];
105100
}
@@ -196,9 +191,14 @@ - (void)sendRequest2:(NSMutableURLRequest *)request
196191
QNInternalProgressBlock progressBlock2 = ^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {
197192
progressBlock(totalBytesWritten, totalBytesExpectedToWrite);
198193
};
199-
__block QNProgessDelegate *delegate = (QNProgessDelegate *)_httpManager.delegate;
194+
__block QNProgessDelegate *delegate = [[QNProgessDelegate alloc] initWithProgress:nil];
200195
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) {
202202

203203
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
204204
double duration = [[NSDate date] timeIntervalSinceDate:startTime];
@@ -221,6 +221,7 @@ - (void)sendRequest2:(NSMutableURLRequest *)request
221221
delegate.cancelBlock = nil;
222222
delegate.progressBlock = nil;
223223
completeBlock(info, resp);
224+
[session finishTasksAndInvalidate];
224225
}];
225226
delegate.task = uploadTask;
226227
delegate.cancelBlock = cancelBlock;

0 commit comments

Comments
 (0)