tags:

views:

6

answers:

1

why we are using NSURLConnection object in xml parsing ie.

for eg:

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

and then NSURLConnection delegate methods invoked
like:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    [connection release];
    [receivedData release];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{  
    [receivedData setLength:0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
    [receivedData appendData:data];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    if(ObjNSXMLParser!=nil && [ObjNSXMLParser retainCount]>0){
        [ObjNSXMLParser release];
        ObjNSXMLParser = nil;
    }

    ObjNSXMLParser = [[NSXMLParser alloc]initWithData:receivedData];
    ObjNSXMLParser.delegate=self;
    [ObjNSXMLParser parse];

}

=============================== But we also get data directly by using: no need to initialize NSURLConnection object:

NSData *dt = [NSData DatawithcontentsofURL:urlname];

which approach is best ?and why ?

A: 

[NSDATA dataWithContentsOfURL:urlname] will block your application while it gets the data.

NSURL will let you get the data in the background so your UI can still work.

If you don't mind blocking the thread you're on, dataWithContentsOfURL is much easier to use. However, if you do that on the main thread you will really annoy the user if the loading takes a long time. They'll think the app has broken!

deanWombourne