如何恢复 ASINetworkQueue?
我正在尝试做的是从网络连接丢失时的同一点恢复和排队。我的问题不在于可达性,而在于恢复下载。我已经度过了整个周末,但运气不佳。
非常感谢任何帮助。
下面的代码具有方法 commenceDownloadIfReachabilityIsOkay
,当网络恢复但进度条不移动时,该方法会被调用,并且我没有看到任何活动来判断下载是否已恢复。
此方法在控制器中调用 frin IBAction
- (void) downloadFile:(UIProgressView*)locprogressView;
{
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReachable startNotifier];
if (![self asiqueue]) {
[self setAsiqueue:[[[ASINetworkQueue alloc] init] autorelease]];
}
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/the_great_american_novel.txt"];
self.request = [ASIHTTPRequest requestWithURL:url];
_progressView=locprogressView;
self.progressView= locprogressView;
[asiqueue cancelAllOperations];
[asiqueue setDownloadProgressDelegate:self.progressView];
[asiqueue setDelegate:self];
[asiqueue setRequestDidFinishSelector:@selector(queueComplete:)];
[asiqueue setShowAccurateProgress:YES];
// [queue setr
[request setDelegate:self];
// [request setDidReceiveDataSelector:@selector(didReceiveData:)];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
[request setAllowResumeForFileDownloads:YES];
[[self asiqueue] addOperation:request]; //queue is an NSOperationQueue
[[self asiqueue]go ];
}
此方法从 checkNetworkStatus
调用,我希望它恢复下载
- (void)commenceDownloadIfReachabilityIsOkay
{
if (self.asiqueue && self.hostActive && self.internetActive)
{
[[self asiqueue]go ];
}
}
此方法只是不断轮询检查网络状态 - 取自如何检查对于一个活跃的iOS 或 OSX 上有互联网连接吗?
- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down.");
self.internetActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
self.internetActive = YES;
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
self.hostActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
self.hostActive = YES;
break;
}
}
[self commenceDownloadIfReachabilityIsOkay];
}
What I am trying to do it to resume and queue from the same point as where in was when network connection was lost . My problem is not with reachability but rather resumption of download. I have spend whole weekend but have had no luck .
Would hugely appreciate any help.
Code below has method commenceDownloadIfReachabilityIsOkay
which does get called when network is resumes but progress bar does not move and I do not see any activity to tell if download has resume.
This method gets called frin IBAction in controller
- (void) downloadFile:(UIProgressView*)locprogressView;
{
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReachable startNotifier];
if (![self asiqueue]) {
[self setAsiqueue:[[[ASINetworkQueue alloc] init] autorelease]];
}
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/the_great_american_novel.txt"];
self.request = [ASIHTTPRequest requestWithURL:url];
_progressView=locprogressView;
self.progressView= locprogressView;
[asiqueue cancelAllOperations];
[asiqueue setDownloadProgressDelegate:self.progressView];
[asiqueue setDelegate:self];
[asiqueue setRequestDidFinishSelector:@selector(queueComplete:)];
[asiqueue setShowAccurateProgress:YES];
// [queue setr
[request setDelegate:self];
// [request setDidReceiveDataSelector:@selector(didReceiveData:)];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
[request setAllowResumeForFileDownloads:YES];
[[self asiqueue] addOperation:request]; //queue is an NSOperationQueue
[[self asiqueue]go ];
}
This method gets called from checkNetworkStatus
and I want it to resume download
- (void)commenceDownloadIfReachabilityIsOkay
{
if (self.asiqueue && self.hostActive && self.internetActive)
{
[[self asiqueue]go ];
}
}
This method just keeps polling to check network status - Taken from How to check for an active Internet connection on iOS or OSX?
- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down.");
self.internetActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
self.internetActive = YES;
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
self.hostActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
self.hostActive = YES;
break;
}
}
[self commenceDownloadIfReachabilityIsOkay];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您的代码,则希望调用:
将导致不久前失败的 ASIHTTPRequest 重新排队并再次开始下载。这不起作用有两个原因:
go
的队列上调用go
确实如此无
解决方案是重新排队请求以重新启动它。
另外,http://allseeing-i.com/ASIHTTPRequest/How-to-use #resuming_interrupted_downloads 说:
您的代码似乎没有这样做。 (您还需要
setDownloadDestinationPath
)If I understand your code correctly, it is hoping that calling:
will result in a ASIHTTPRequest that failed sometime ago getting requeued and starting download again. There's two reasons this won't work:
go
on a queue thatgo
has already been called on doesnothing
The solution is to requeue the request to restart it.
Also, http://allseeing-i.com/ASIHTTPRequest/How-to-use#resuming_interrupted_downloads says:
which your code doesn't seem to be doing. (You also need to
setDownloadDestinationPath
)