title: Requesting data from Web Server on iPhone creates too much of a delay
body: <p>In my iPhone app, I need to access the webserver to login into my system. The client requires that the database be on server only. So I have made an admin panel in ASP.NET and MySQL database where in I store the login information.</p>
  
  <p>Now when I use the method given below in the code to fetch the data from server then it takes too much of time (i.e. More than a minute.) to respond. </p>
  
  <p>In the case where I have fetched a particular data from server before, then that value is fetched fast when I try to fetch it the next time. But for new data it again takes lots of time or rather times out.</p>
  
  <p>Sometimes the request even times out.</p>
  
  <p>What should I do to decrease the delay and speed up the whole login process?</p>
  
  <p>Is there a better way to do this?</p>
  
  <p><strong>Info regarding Code below:</strong> </p>
  
  <p>firstname.text is my Username.</p>
  
  <p>lastname.text is my Password.</p>
  
  <p>label.text is a temporary label that I use to store the password obtained from server.</p>
  
  <p>allUsers is the array where the response obtained from server is stored.</p>
  
  <p>Also I am using JSON to parse the data between webserver and iPhone.</p>
  
  <p>I use a plist to store my url. Plist name is <strong>url.plist</strong>.</p>
  
  <p><strong>Code is as Below:</strong> </p>
  
  <pre><code>    SBJSON *json = [SBJSON new];
      json.humanReadable = YES;
      NSString *service = @"/getUserInfo";
      //NSString *requestString = [NSString stringWithFormat:@"{\"method\":\"%@\"}",  service];
      NSString *requestString = [NSString stringWithFormat:@"{\"firstname\":\"%@\"}",firstName.text,nil];
      NSLog(@"Request String: %@", requestString);
  
      NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
  
      NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"url" ofType:@"plist" ];
      NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
      NSString *urlLoc = [fileContents objectForKey:@"baseURL"];
      urlLoc = [urlLoc stringByAppendingString:service];
      NSLog(@"URL is %@",urlLoc);
      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: 
                                      [NSURL URLWithString: urlLoc]];  
  
      NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
      [request setHTTPMethod: @"POST"];
      [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
      [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
      [request setHTTPBody: requestData];
  
      //Data returned by WebService
      NSError *respError = nil; 
      NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &amp;respError ];
  
      if (respError) {
          NSString *msg = [NSString stringWithFormat:@"Connection failed! Error - %@ %@",
                           [respError localizedDescription],
                           [[respError userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]];   
  
          UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Check your network connection" 
                                                              message:msg delegate:self cancelButtonTitle:@"OK" 
                                                    otherButtonTitles:nil];
          [alertView show];
  
  
          NSArray *keys = [NSArray arrayWithObjects:@"firstname", @"lastname", nil];
          NSArray *objects = [NSArray arrayWithObjects:@"failed to", @"refresh data...", nil];
          NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
  
          allUsers = [[NSArray alloc] initWithObjects:dictionary, nil];
          //[self setUserData:allUsers];
          //[tblView reloadData];
  
          //[allUsers release];
  
      } 
      else
      {
  
          NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
  
          NSDictionary *results = [responseString JSONValue];
          // Additional steps as the webservice is adding an additional "{d:" so stripping of that
          NSString *extractUsers = [results objectForKey:@"d"];
  
          // The actual string that Web services returned, so re-scan the same and convert it as object
          NSDictionary *finalResult = [extractUsers JSONValue];
          allUsers = [finalResult objectForKey:@"users"];
          NSLog(@"Data is : %@",allUsers);
          NSLog(@"Final Value is : %@",[[allUsers objectAtIndex:0] valueForKey:@"lastname"]);
          if([allUsers count]&gt;0)
          {
              label.text = [[allUsers objectAtIndex:0] valueForKey:@"lastname"];
          }
          else 
          {
              label.text = @"";
          }
          [responseString release];
          [request release];
  
      }
  
  
  
  
      [inProgressIndicator stopAnimating];
      [fileContents release];
  
      //Release all the allocated data
      [json release];
  
  }
  </code></pre>