1

I am fetching data using POST method. And I have successfully retrieved all the data.It's taking too long to display it in UI but I can print it immediately on console, my code is

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.xxxyyy.com/v1/api/client/authorize"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"ABCD" forHTTPHeaderField:@"Authkey"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

 NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 
{
    NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Authkey"];

    NSData* data1 = [requestReply dataUsingEncoding:NSUTF8StringEncoding];
    jsonReturnArray = [NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingAllowFragments error:&error];

    NSArray *array = [jsonReturnArray copy];
    [self rec:array];

    NSString *phoneNumber=[NSString stringWithFormat:@"%@",[jsonReturnArray valueForKey:@"phone"]];
    lblPhoneNumber.text = phoneNumber;

    NSString *Address=[NSString stringWithFormat:@"%@ %@ %@,CA %@",[jsonReturnArray valueForKey:@"street1"],[jsonReturnArray valueForKey:@"street2"],[jsonReturnArray valueForKey:@"city"],[jsonReturnArray valueForKey:@"postalcode"]];
    lblAddress.text=Address;//takes long time to display
    NSLog(@"%@",Address);//immeaditely print

    strlatitude=[jsonReturnArray valueForKey:@"latitude"];
    strlongitude=[jsonReturnArray valueForKey:@"longitude"];

    [self Map:(MKMapView *)mapLocation didUpdateUserLocation:(MKUserLocation *)nil];//method call
}] resume];
Fayza Nawaz
  • 2,256
  • 3
  • 26
  • 61
Ragul
  • 3,176
  • 3
  • 16
  • 32

2 Answers2

0

This is take too time to print data, but if you use NSURLConnection class it may be help you.This is my Class method it may be helpful.

+ (void)postRequestData:(NSDictionary *)postVars
                 Action:(APIMode)action
  WithCompletionHandlar:(void (^) (id result, BOOL status))completionBlock
{

    NSURL *url = [NSURL URLWithString:API_URL([self getAPINameForType:action])];
    NSLog(@"Request URL %@",[NSString stringWithFormat:@"%@",url]);
    NSString *contentType = @"application/json";
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSError *err = nil;

    NSMutableDictionary *params=[[NSMutableDictionary alloc] initWithDictionary:postVars];
    //  [params setObject:[self getAPINameForType:action] forKey:@"mode"];
    NSLog(@"Paramater %@",params);

    NSData *body = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:&err];
    [request setHTTPBody:body];
    [request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)body.length] forHTTPHeaderField: @"Content-Length"];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
     {
         if(!connectionError)
         {
             NSError *error = nil;

             NSDictionary *dictResponse = [NSDictionary dictionaryWithDictionary:[NSJSONSerialization JSONObjectWithData:data
                                                                                                                 options:NSJSONReadingAllowFragments error:&error]];

             dispatch_async(dispatch_get_main_queue(), ^{
                 completionBlock(dictResponse,(error == nil));
             });

             NSLog(@"%@",dictResponse);
         }
         else
         {
             dispatch_async(dispatch_get_main_queue(), ^{
                 completionBlock(connectionError.localizedDescription,NO);
             });
         }
     }];
}

Use this method instead of it.It is executed fast because NSURLConnection Class execute in background.

yankit Patel
  • 331
  • 1
  • 12
0

Try to fetch your data using NSURLConnection class(manual code) or simply use AFNetworking class(less code). AFNetworking internally uses NSURLConnection class itself.

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143