0

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.

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.

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.

Sometimes the request even times out.

What should I do to decrease the delay and speed up the whole login process?

Is there a better way to do this?

Info regarding Code below:

firstname.text is my Username.

lastname.text is my Password.

label.text is a temporary label that I use to store the password obtained from server.

allUsers is the array where the response obtained from server is stored.

Also I am using JSON to parse the data between webserver and iPhone.

I use a plist to store my url. Plist name is url.plist.

Code is as Below:

    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: &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]>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];

}
halfer
  • 19,824
  • 17
  • 99
  • 186
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • As Nickolay says, the problem could be server side. For instance, the MySQL server might be configured to start on demand which means the first time you use the service, it has to start the database server. I would start by making sure that it is not the service that is the issue. You can do this by making sure that it will also respond to a GET request with the lookup key as a parameter and then simply pointing a web browser at it. – JeremyP Jan 12 '11 at 12:04

1 Answers1

1

At first, you should check on which side delay is taking place - is it server's lag (maybe, not optimized DB usage or so on), internet-connection issue or another problem. Just trace and check, which line of code is executed for that minute at first.

Nickolay Olshevsky
  • 13,706
  • 1
  • 34
  • 48
  • my server is on my localhost. So I dont think there should be any issues with speed of the internet. So can you please refer the code I have posted above and let me know what could be causing this delay? – Parth Bhatt Jan 12 '11 at 10:38
  • I doesn't see anything what can cause a delay there, so I think that issue is on the server's side. – Nickolay Olshevsky Jan 12 '11 at 11:02
  • I tried debugging the code and I found that this line creates the delay: NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ]; What could be wrong? – Parth Bhatt Jan 12 '11 at 12:12
  • Have you checked, how fast server produces the responce? – Nickolay Olshevsky Jan 12 '11 at 12:35
  • As it is a localhost,so how do I judge how fast or slow it is? – Parth Bhatt Jan 12 '11 at 13:02