0

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];
}
Community
  • 1
  • 1
Java Ka Baby
  • 4,482
  • 10
  • 36
  • 50

1 Answers1

2

If I understand your code correctly, it is hoping that calling:

[[self asiqueue]go ];

will result in a ASIHTTPRequest that failed sometime ago getting requeued and starting download again. There's two reasons this won't work:

  1. Once an ASIHTTPRequest has failed, it's removed from the queue.
  2. Calling go on a queue that go has already been called on does nothing

The solution is to requeue the request to restart it.

Also, http://allseeing-i.com/ASIHTTPRequest/How-to-use#resuming_interrupted_downloads says:

Additionally, you must set a temporary download path yourself (setTemporaryFileDownloadPath), with the path of the partial data.

which your code doesn't seem to be doing. (You also need to setDownloadDestinationPath )

JosephH
  • 36,107
  • 19
  • 126
  • 149
  • wow yes thats exactly what I was expecting ofcourse incorrectly . Do I need to set `setDownloadDestinationPath ` even for ios. What can be a possible value since simulator can not access machine disk structure. – Java Ka Baby Nov 06 '11 at 20:22
  • Yes, you need to set a valid path for iOS. iOS has a full file system - depending on what the file is, you may want to store it in your documents directory under your app, the code will be something like `NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);` – JosephH Nov 07 '11 at 11:44