简体   繁体   English

在ios中异步调用Web服务

[英]Calling a web service Asynchronously in ios

    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. 到目前为止,我正在调用服务并在tableview中加载之后将数据完全获取到数组中。但是当有巨大数据时,它需要很长时间才能运行,所以我如何异步调用服务并一次加载tableview 。

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…. 我想通过Example进行更好的理解。就像跟随Xml响应一样,如果它可以解析1000个学生,那么我想获取第一个学生详细信息并在tableview中加载,第二个学生详细信息并在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. 提前致谢。

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]);
    }
}];

Before you get any data, show a message or a loading indicator. 在获取任何数据之前,请显示一条消息或加载指示器。 Here's a decent SO post on how to do it. 这是有关如何做到的体面的SO帖子

Whenever you get your data, call reloadData on your tableview, and it will rebuild itself from the delegate methods numberOfRowsInSection , cellForRowAtIndexPath , etc. 每当获取数据时,请在表reloadData上调用reloadData ,它将从委托方法numberOfRowsInSectioncellForRowAtIndexPath等重建自身。

-(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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM