views:

46

answers:

1

Me again! I am using NSURL to get a file then parse it. I have been looking for a couple of hours now on a progressbar i am trying to implement in my app. I know I first need to get the file size, then keep updating how much data i've downloaded as I continue to pull. I have seen the example using "ASIHTTPRequest" but is there a way to do it with what I already have?

Here is where I start the download.

-(void)parseNewData {


    //start network activity spinner and release controller when done
    parserDone = NO;
    [root downloadIcon];


    //create pool to avoid memory leak
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];


    // get the XML path and start parsing
    NSURL *pathURL = [NSURL URLWithString:@"http://www.mysite.com/myfile.xml"];




    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:pathURL];
    [parser setDelegate:self];
    [parser parse];

    //drain pool 
    [pool drain];
    [pool release];

}

Can someone point me in the right direction on how to get file size, then how to update how much I've downloaded. Thanks in advance!

A: 

You need to use NSURLConnection if you want to get the file size and the progress. You get delegate methods which you can use to monitor progress. The didSendBodyData: delegate method tells you how much data there is in bytes. The connectionDidFinishLoading is where you uget receivedData to use in your NSXMLParser code.

NSURLRequest *theRequest = [NSURLRequest requestWithURL:URL    cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {

    receivedData = [[NSMutableData data] retain];
}

}
  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
// 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.

// receivedData is an instance variable declared elsewhere.
[receivedData setLength:0];
}
 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
// release the connection, and the data object

// inform the user
NSLog(@"Connection failed! Error - %@ %@",
      [error localizedDescription],
      [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
[connection release];
// receivedData is declared as a method instance elsewhere
[receivedData release];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
Robert Redmond
thanks for the suggestion. I will have to revamp some code but you have steered me in the right direction. Get the data, then parse... instead of parse as the data comes in!GOT IT! :)
Louie