2020@interface QNProgessDelegate : NSObject
2121- (void )observeValueForKeyPath : (NSString *)keyPath ofObject : (id )object change : (NSDictionary *)change context : (void *)context ;
2222@property (nonatomic , strong ) QNInternalProgressBlock progressBlock;
23- - (instancetype ) initWithProgress : (QNInternalProgressBlock)progressBlock ;
23+ @property (nonatomic , strong ) NSProgress *progress;
24+ - (instancetype )initWithProgress : (QNInternalProgressBlock)progressBlock ;
2425@end
2526
2627@implementation QNProgessDelegate
27- - (instancetype ) initWithProgress : (QNInternalProgressBlock)progressBlock {
28- if (self = [super init ]) {
29- _progressBlock = progressBlock;
30- }
31-
32- return self;
28+ - (instancetype )initWithProgress : (QNInternalProgressBlock)progressBlock {
29+ if (self = [super init ]) {
30+ _progressBlock = progressBlock;
31+ _progress = nil ;
32+ }
33+
34+ return self;
3335}
3436
3537- (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 );
38+ if (context == nil || object == nil ) {
39+ return ;
40+ }
41+
42+ NSProgress *progress = (NSProgress *)object;
43+
44+ void *p = (__bridge void *)(self);
45+ if (p == context) {
46+ _progressBlock (progress.completedUnitCount , progress.totalUnitCount );
47+ }
48+ else {
49+ [super observeValueForKeyPath: keyPath ofObject: object change: change context: context];
50+ }
4851}
4952
5053@end
@@ -82,11 +85,18 @@ + (QNResponseInfo *)buildResponseInfo:(NSHTTPURLResponse *)response
8285 QNResponseInfo *info;
8386
8487 if (response) {
88+ int status = (int )[response statusCode ];
8589 NSDictionary *headers = [response allHeaderFields ];
8690 NSString *reqId = headers[@" X-Reqid" ];
8791 NSString *xlog = headers[@" X-Log" ];
88- int status = (int )[response statusCode ];
89- info = [[QNResponseInfo alloc ] init: status withReqId: reqId withXLog: xlog withHost: host withDuration: duration withBody: body];
92+ NSString *xvia = headers[@" X-Via" ];
93+ if (xvia == nil ) {
94+ xvia = headers[@" X-Px" ];
95+ }
96+ if (xvia == nil ) {
97+ xvia = headers[@" Fw-Via" ];
98+ }
99+ info = [[QNResponseInfo alloc ] init: status withReqId: reqId withXLog: xlog withXVia: xvia withHost: host withDuration: duration withBody: body];
90100 }
91101 else {
92102 info = [QNResponseInfo responseInfoWithNetError: error host: host duration: duration];
@@ -100,15 +110,14 @@ - (void) sendRequest:(NSMutableURLRequest *)request
100110 __block NSDate *startTime = [NSDate date ];
101111 NSProgress *progress = nil ;
102112 __block NSString *host = request.URL .host ;
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) {
113+
114+ if (progressBlock == nil ) {
115+ progressBlock = ^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {
116+ };
117+ }
118+ __block QNProgessDelegate *delegate = [[QNProgessDelegate alloc ] initWithProgress: progressBlock];
119+
120+ NSURLSessionUploadTask *uploadTask = [_httpManager uploadTaskWithStreamedRequest: request progress: &progress completionHandler: ^(NSURLResponse *response, id responseObject, NSError *error) {
112121 NSData *data = responseObject;
113122 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
114123 double duration = [[NSDate date ] timeIntervalSinceDate: startTime];
@@ -124,11 +133,17 @@ - (void) sendRequest:(NSMutableURLRequest *)request
124133 else {
125134 info = [QNSessionManager buildResponseInfo: httpResponse withError: error withDuration: duration withResponse: data withHost: host];
126135 }
127- NSLog (@" finish %p %p " , progress, delegate);
128- [progress removeObserver: delegate forKeyPath: @" fractionCompleted" context: (__bridge void *)(delegate)];
129- completeBlock (info, resp);
136+
137+ if (delegate.progress != nil ) {
138+ [delegate.progress removeObserver: delegate forKeyPath: @" fractionCompleted" context: (__bridge void *)(delegate)];
139+ delegate.progress = nil ;
140+ }
141+ completeBlock (info, resp);
130142 }];
131- [progress addObserver: delegate forKeyPath: @" fractionCompleted" options: NSKeyValueObservingOptionNew context: (__bridge void *)(progressBlock)];
143+ if (progress != nil ) {
144+ [progress addObserver: delegate forKeyPath: @" fractionCompleted" options: NSKeyValueObservingOptionNew context: (__bridge void *)delegate];
145+ delegate.progress = progress;
146+ }
132147
133148 [request setTimeoutInterval: kQNTimeoutInterval ];
134149
@@ -177,17 +192,13 @@ - (void) post:(NSString *)url
177192 [request setValuesForKeysWithDictionary: params];
178193 }
179194 [request setHTTPBody: data];
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 ; {
195+ QNAsyncRun ( ^{
196+ [self sendRequest: request
197+ withCompleteBlock: completeBlock
198+ withProgressBlock: progressBlock];
199+ });
188200}
189201
190-
191202@end
192203
193204#endif
0 commit comments