Search code examples
jsonuitableviewios7

Which is the best method to fetch data from JSON url?


I have a application in which when we login a new view controller will be loaded which is a table view and data need to be fetched from server by Json call.Which is the best method to use in this situation.

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://YourURL.com/FakeURL/PARAMETERS"]];
[request setHTTPMethod:@"GET"];

NSURLResponse *requestResponse;
NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil];

NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);

or

-(void)update{
NSString *urlAsString = [NSString stringWithFormat:@"http://YourURL.com/FakeURL/PARAMETERS"];
    NSURL *url = [[NSURL alloc] initWithString:urlAsString];
    self.responseData = [NSMutableData data];
    NSURLRequest *request = [NSURLRequest requestWithURL:
                             url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"didReceiveResponse");
    [self.responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"didFailWithError");
    NSString *errorDisplay = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
    NSLog(@"%@",errorDisplay);
}

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

    NSLog(@"connectionDidFinishLoading");
    NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);

    // convert to JSON
    NSError *myError = nil;
    NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];

}

Or i should use Asynchronous Method

-(void)updateCheckList{


    self.checkListresponseData = [NSMutableData data] ;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/outing-service-web/checkList/all"]];//asynchronous call
   checkListConn = [[NSURLConnection alloc] initWithRequest:request delegate:self];



}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

        NSLog(@"didReceiveResponse");
        [self.checkListresponseData setLength:0];



}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

        [self.checkListresponseData appendData:data];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"didFailWithError");
    NSString *errorDisplay = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
    NSLog(@"%@",errorDisplay);

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Cannot connect to server" delegate:nil cancelButtonTitle:nil otherButtonTitles: @"Retry",@"Close",nil];
    [alertView show];

}


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

    if (connection == checkListConn) {
        NSLog(@"connectionDidFinishLoading");
        NSLog(@"Succeeded! Received %d bytes of data",[self.checkListresponseData length]);


        // convert to JSON
        NSError *myError = nil;
        NSArray *res = [NSJSONSerialization JSONObjectWithData:self.checkListresponseData options:NSJSONReadingMutableLeaves error:&myError];


    }




}

Solution

  • If you do it asynchronously, then the process will be done in the background. In short, synchronous requests block the execution of code which creates "freezing" on the screen and an unresponsive user experience.

    Check the following URL, it might get you more clear idea.

    Synchronous and Asynchronous Requests