1
    NSData *responseData2= [NSURLConnection sendSynchronousRequest:request2 returningResponse:&urlResponse2 error:&error2];
    aparser =[[NSXMLParser alloc]initWithData:responseData2];

Up to now I am calling a service and getting data completely in an array,after that loading in tableview.but when there is a huge data it’s taking long time to run, so how can i call service asynchronously and load the tableview at a time.

I want to go with Example for better understanding.Like following Xml response, if it has 1000 students to parse,so i want to get first student details and load it in tableview and second student details and load in Tableview…. and so on..

  <Class>
            <Student>
            <Name>Rama</Name>
            <Rollno>01</Rollno>
            </Student>
            <Student>
            <Name>Ravi</Name>
            <Rollno>02</Rollno>
            </Student>
         ......
         ......
  </Class>

Thanks in advance.

siva
  • 251
  • 2
  • 16

3 Answers3

0

Like so

NSOperationQueue *mainQueue = [[NSOperationQueue alloc] init];
[mainQueue setMaxConcurrentOperationCount:5];

NSURL *url = [NSURL URLWithString:@"http://192.168.0.63:7070/api/Misc/GetFuelTypes"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

[request setHTTPMethod:@"GET"];
[request setAllHTTPHeaderFields:@{@"Accepts-Encoding": @"gzip", @"Accept": @"application/json"}];

[NSURLConnection sendAsynchronousRequest:request queue:mainQueue completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) {
    NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response;
    if (!error) {
        NSLog(@"Status Code: %li %@", (long)urlResponse.statusCode, [NSHTTPURLResponse localizedStringForStatusCode:urlResponse.statusCode]);
        NSLog(@"Response Body: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}
else {
    NSLog(@"An error occured, Status Code: %i", urlResponse.statusCode);
    NSLog(@"Description: %@", [error localizedDescription]);
    NSLog(@"Response Body: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
    }
}];
mntruell
  • 181
  • 1
  • 13
  • Thanks for ur response for xml response is it the same way, and also how to load the tableview asynchronosuly.. – siva Aug 26 '14 at 12:09
  • @siva it is sendAsynchronousRequest instead of sendSynchronous request and you do the table stuff in the handler which gets call when the data is recieved – mntruell Aug 26 '14 at 19:28
  • here the completion handle is getting called after loading total data, but i was thinking getting fisrt stdent table view and load in Tableview and second student etc. but here i am getting total 1000 student details at time and and taking too much time in this also. – siva Aug 27 '14 at 10:19
  • may be what i am thinking is not possible?isn't it? and also wht is [mainQueue setMaxConcurrentOperationCount:5]; what is the use of this – siva Aug 27 '14 at 10:32
0

Before you get any data, show a message or a loading indicator. Here's a decent SO post on how to do it.

Whenever you get your data, call reloadData on your tableview, and it will rebuild itself from the delegate methods numberOfRowsInSection, cellForRowAtIndexPath, etc.

Community
  • 1
  • 1
Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
-1
-(void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib.

    app=[UIApplication sharedApplication].delegate;

    xmldata=[[NSMutableData alloc]init];
    dict=[[NSDictionary alloc]init];
    mArray=[[NSMutableArray alloc]init];
    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://indianbloodbank.com/api/donors/?bloodgroup=O%2B"]];
    NSURLConnection *Co=[NSURLConnection connectionWithRequest:request delegate:self];
    NSLog(@"Connection-%@",Co);
}

-(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response{return request;} -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"NETWORK ERROR"); }

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"%@",response); } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {[xmldata appendData:data]; NSLog(@"XMLData-%@",xmldata); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { dict=[XMLReader dictionaryForXMLData:xmldata error:nil]; NSLog(@"Dictionary-%@",dict); mArray=[dict retrieveForPath:@"response.donorslist.donors"]; NSLog(@"MutableArray-%@",mArray);

//DONORS ENTITY

    //Saving the data in to database
    for (int i=0; i<mArray.count; i++)
    {
        Database * don=[NSEntityDescription insertNewObjectForEntityForName:@"Donors" inManagedObjectContext:app.managedObjectContext];

        don.donid=[[mArray objectAtIndex:i]valueForKey:@"id"];
        don.gender=[[mArray objectAtIndex:i]valueForKey:@"gender"];
        don.name=[[mArray objectAtIndex:i]valueForKey:@"name"];
        don.location=[[mArray objectAtIndex:i]valueForKey:@"location"];
        don.phone=[[mArray objectAtIndex:i]valueForKey:@"phone"];

        [app saveContext];

        NSLog(@"Donors Entity Details-%@,%@,%@,%@,%@",[[mArray objectAtIndex:i]valueForKey:@"id"],[[mArray objectAtIndex:i]valueForKey:@"gender"],[[mArray objectAtIndex:i]valueForKey:@"name"],[[mArray objectAtIndex:i]valueForKey:@"location"],[[mArray objectAtIndex:i]valueForKey:@"phone"]);

        [tbl reloadData];
    }
}
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65