Skip to content

Commit d9854fd

Browse files
committed
nsprogress
1 parent 0e9a29f commit d9854fd

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

QiniuSDK/Http/QNSessionManager.m

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,42 @@
1313
#import "QNUserAgent.h"
1414
#import "QNResponseInfo.h"
1515
#import "QNDns.h"
16+
#import "QNAsyncRun.h"
1617

1718
#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000) || (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090)
1819

20+
@interface QNProgessDelegate : NSObject
21+
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
22+
@property (nonatomic, strong) QNInternalProgressBlock progressBlock;
23+
- (instancetype) initWithProgress:(QNInternalProgressBlock)progressBlock;
24+
@end
25+
26+
@implementation QNProgessDelegate
27+
- (instancetype) initWithProgress:(QNInternalProgressBlock)progressBlock{
28+
if (self = [super init]) {
29+
_progressBlock = progressBlock;
30+
}
31+
32+
return self;
33+
}
34+
35+
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; {
36+
NSLog(@"log %@ %@, %@, %p", keyPath, object, change, context);
37+
if (context == nil || object == nil) {
38+
return;
39+
}
40+
41+
NSProgress *progress = (NSProgress *)object;
42+
43+
void *p = (__bridge void *)(self);
44+
if (p != context) {
45+
return;
46+
}
47+
_progressBlock(progress.completedUnitCount, progress.totalUnitCount);
48+
}
49+
50+
@end
51+
1952
@interface QNSessionManager ()
2053
@property (nonatomic) AFHTTPSessionManager *httpManager;
2154
@end
@@ -67,8 +100,15 @@ - (void) sendRequest:(NSMutableURLRequest *)request
67100
__block NSDate *startTime = [NSDate date];
68101
NSProgress *progress = nil;
69102
__block NSString *host = request.URL.host;
70-
71-
NSURLSessionUploadTask *uploadTask = [_httpManager uploadTaskWithStreamedRequest:request progress:&progress completionHandler: ^(NSURLResponse *response, id responseObject, NSError *error) {
103+
104+
__block QNProgessDelegate *delegate = nil;
105+
if (progressBlock == nil) {
106+
progressBlock = ^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {
107+
};
108+
}
109+
delegate = [[QNProgessDelegate alloc] initWithProgress:progressBlock];
110+
111+
NSURLSessionUploadTask *uploadTask = [_httpManager uploadTaskWithStreamedRequest:request progress:&progress completionHandler: ^(NSURLResponse *response, id responseObject, NSError *error) {
72112
NSData *data = responseObject;
73113
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
74114
double duration = [[NSDate date] timeIntervalSinceDate:startTime];
@@ -84,10 +124,11 @@ - (void) sendRequest:(NSMutableURLRequest *)request
84124
else {
85125
info = [QNSessionManager buildResponseInfo:httpResponse withError:error withDuration:duration withResponse:data withHost:host];
86126
}
87-
completeBlock(info, resp);
88-
[progress removeObserver:self forKeyPath:@"completedUnitCount" context:(__bridge void *)(progressBlock)];
127+
NSLog(@"finish %p %p", progress, delegate);
128+
[progress removeObserver:delegate forKeyPath:@"fractionCompleted" context:(__bridge void *)(delegate)];
129+
completeBlock(info, resp);
89130
}];
90-
[progress addObserver:self forKeyPath:@"completedUnitCount" options:NSKeyValueObservingOptionNew context:(__bridge void *)(progressBlock)];
131+
[progress addObserver:delegate forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:(__bridge void *)(progressBlock)];
91132

92133
[request setTimeoutInterval:kQNTimeoutInterval];
93134

@@ -96,17 +137,6 @@ - (void) sendRequest:(NSMutableURLRequest *)request
96137
[uploadTask resume];
97138
}
98139

99-
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
100-
if (context == nil) {
101-
return;
102-
}
103-
NSProgress *progress = (NSProgress *)object;
104-
QNInternalProgressBlock progressBlock = (__bridge QNInternalProgressBlock)context;
105-
if (progress != nil && progressBlock != nil) {
106-
progressBlock(progress.completedUnitCount, progress.totalUnitCount);
107-
}
108-
}
109-
110140
- (void)multipartPost:(NSString *)url
111141
withData:(NSData *)data
112142
withParams:(NSDictionary *)params
@@ -147,11 +177,17 @@ - (void) post:(NSString *)url
147177
[request setValuesForKeysWithDictionary:params];
148178
}
149179
[request setHTTPBody:data];
150-
[self sendRequest:request
151-
withCompleteBlock:completeBlock
152-
withProgressBlock:progressBlock];
180+
QNAsyncRun(^{
181+
[self sendRequest:request
182+
withCompleteBlock:completeBlock
183+
withProgressBlock:progressBlock];
184+
});
185+
}
186+
187+
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; {
153188
}
154189

190+
155191
@end
156192

157193
#endif

0 commit comments

Comments
 (0)