• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSURLConnection percentage calculation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSURLConnection percentage calculation


  • Subject: NSURLConnection percentage calculation
  • From: Milen Dzhumerov <email@hidden>
  • Date: Thu, 11 May 2006 23:25:01 +0100

Hi list,

I've been playing with NSURLConnection to request an URL (async) and everything has been working fine. I'm also calculating the percentage loaded while the transfer is active and displaying it in one of the delegate. The problem is that sometimes the reported expected content length is not what actually is transferred so I get percentages bigger than 100. Here are some details about my implementation. I have two instance variable declared as:

long long contentLength;
long long totalRead;

The implementation of the relevant delegates is shown below:

- (void)connection:(NSURLConnection *)connection didReceiveData: (NSData *)data
{
// append the new data to the receivedData
NSLog(@"Appending %d bytes...", [data length]);
// increment total read bytes
totalRead += (long long) [data length];
// calculate ration
double ln = (((double)totalRead)/((double)contentLength));
// format the ration nicely
char buff[30];
sprintf(buff, "%0.2f", ln);
double pc = atof(buff);
pc *= 100;
// log % and append data
NSLog(@"Percentage: %3.0f%%", pc);
[receivedData appendData:data];
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response
{
gettimeofday(&t1, NULL);
// this method is called when the server has determined that it
// has enough information to create the NSURLResponse

// it can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.


	// store content length
	contentLength = (long long) [response expectedContentLength];
	NSLog(@"%qi", contentLength);

	[receivedData setLength:0];
	totalRead = 0;
}

-(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse: (NSURLResponse *)redirectResponse
{
[connection cancel];
return nil;
}


In my - (void)connectionDidFinishLoading:(NSURLConnection *) connection delegate I have a statement which logs the following:
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
And that number is _sometimes_ bigger than the expected length so all my calculates go wrong in these causes. I have to note that this occurs on the same URLs (differences in expected length and actual happens on apple.com every-time and does not on eg. planetkde.org). Any pointers on where I'm going wrong are appreciated. Thanks for reading this email.


Kind regards,
Milen Dzhumerov

Email: email@hidden
Web: http://www.1nsp1r3d.co.uk/



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: creating NSCalendarDate with a specific time zone
  • Next by Date: Undefined symbols error in deployment build
  • Previous by thread: Core Data delayed notification after sleep
  • Next by thread: Undefined symbols error in deployment build
  • Index(es):
    • Date
    • Thread