簡體   English   中英

在ios中異步調用Web服務

[英]Calling a web service Asynchronously in ios

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

到目前為止,我正在調用服務並在tableview中加載之后將數據完全獲取到數組中。但是當有巨大數據時,它需要很長時間才能運行,所以我如何異步調用服務並一次加載tableview 。

我想通過Example進行更好的理解。就像跟隨Xml響應一樣,如果它可以解析1000個學生,那么我想獲取第一個學生詳細信息並在tableview中加載,第二個學生詳細信息並在Tableview中加載…。 等等..

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

提前致謝。

像這樣

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

在獲取任何數據之前,請顯示一條消息或加載指示器。 這是有關如何做到的體面的SO帖子

每當獲取數據時,請在表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