views:

62

answers:

0

Hi

I'm trying to get the time interval I need to download/upload something to/from a server with no password/proxy. I found a method used by ericasadun. Which actually prints the time iPhone needed to download a certain file.

 - (void) getData: (NSNumber *) which
    {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        self.log = [NSMutableString string];
        [self doLog:@"Downloading data now...\n"];
        NSDate *date = [NSDate date];

        NSArray *urlArray = [NSArray arrayWithObjects: SMALL_URL, BIG_URL, FAKE_URL, nil];
        NSURL *url = [NSURL URLWithString: [urlArray objectAtIndex:[which intValue]]];
        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
        NSURLResponse *response;
        NSError *error;
        NSData* result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
        [self doLog:@"Response expects %d bytes", [response expectedContentLength]];
        [self doLog:@"Response suggested file name: %@", [response suggestedFilename]];
        if ([response suggestedFilename])
            self.savePath = [DEST_PATH stringByAppendingString:[response suggestedFilename]];

        if (!result)
            [self doLog:@"Error downloading data: %@.", [error localizedDescription]];
        else if ([response expectedContentLength] < 0)
            [self doLog:@"Error with download. Carrier redirect?"];
        else
        {
            //NSLog(@"Elapsed time: %0.2f seconds.", -1*[date timeIntervalSinceNow]);

            //textView.text = @"Elapsed time: %0.2f seconds.", -1*[date timeIntervalSinceNow];

            [self doLog:@"Download succeeded."];
            [self doLog:@"Read %d bytes", result.length];
            [self doLog:@"Elapsed time: %0.2f seconds.", -1*[date timeIntervalSinceNow]];
            [result writeToFile:self.savePath atomically:YES];
            [self doLog:@"Data written to file: %@.", self.savePath];
        }

        [self performSelectorOnMainThread:@selector(finishedGettingData) withObject:nil waitUntilDone:NO];
        [pool release];
    }

I'd like to know if in your opinion this is the best way to do it (also for FTP connections).

Also, about upload, is there any similar solution? I thought on

- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
and to get the time needed with the same solution of above.

Are these the right directions both for http sand ftp? I'm not asking for code or something similar, just for someone saying to me, this is the right direction!

Thanks!