I use NSURLSessionTask
我正在尝试监视我的一些 HTTP 请求需要多长时间,我可以监视什么委托方法(或其他方法)NSURLSessionTask
实际上提出了最初的请求?如果这是一个NSURLConnection
里面一个NSOperation
我只是在开始请求时启动计时器,但我无法控制任务何时开始。
请检查NSURLSessionTaskDelegate。它具有以下委托回调:
URLSession:task:didCompleteWithError:
URLSession:task:didReceiveChallenge:completionHandler:
URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:
URLSession:task:needNewBodyStream:
URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:
来计算时间间隔。
选项 01 [大约]:
您应该在调用resume方法后立即启动一个计时器,并计算何时调用委托回调didCompleteWithError。
self.dataTask = [self.session dataTaskWithRequest:theRequest];
[self.dataTask resume];
NSTimeInterval totalCountdownInterval;
NSDate* startDate = [NSDate date];
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkCountdown:) userInfo:nil repeats:YES];
选项 02 [如果需要精度]:
NSURLSessionTask 的属性都是 KVO 兼容的。
[self.dataTask addObserver:self forKeyPath:@"someKeyPath" options:NSKeyValueObservingOptionOld context:nil];
[self.dataTask resume];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
// do some calculation after checking state
/*
NSURLSessionTaskStateRunning = 0,
NSURLSessionTaskStateSuspended = 1,
NSURLSessionTaskStateCanceling = 2,
NSURLSessionTaskStateCompleted = 3, */
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)