1

I am trying to load data from server into id result variable, my url is working perfectly i can see data on the browser, but the data loading process is very slow(15 secs ) and the result getting Output data id result is nil

Class: MyWebservices :-

-(id)getResponseFromServer:(NSString*)requestString
{
  id result;
  NSError *error;
NSURLResponse *response = nil;
NSURL *url = [NSURL URLWithString:[requestString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    NSData * resultData= [[NSData alloc]init];
    resultData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error]; 

Class: WebserviceCallingClass

 - (void)viewDidLoad
  {

id result =  [AppDelegate.MyWebservices  getResponseFromServer:urlString] ;

}

YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48
Vicky iOS
  • 35
  • 11

1 Answers1

1

Use asynchronous request.

1) Use NSURLConnectionDelegate and declares in your interface class a:

NSMutableData *_responseData;

2)Send your asynchronous request and set in timeout interval a time major than 15 seconds

 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://uri"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
        conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
        [conn scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
        [conn start];

3) Implement your delegate methods

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        // A response has been received, this is where we initialize the instance var you created
        // so that we can append data to it in the didReceiveData method
        // Furthermore, this method is called each time there is a redirect so reinitializing it
        // also serves to clear it

        _responseData = [[NSMutableData alloc] init];
    }

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        // Append the new data to the instance variable you declared
        [_responseData appendData:data];
    }

    - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                      willCacheResponse:(NSCachedURLResponse*)cachedResponse {
        // Return nil to indicate not necessary to store a cached response for this connection
        //NSLog(@"cache");
        return nil;
    }

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        // The request is complete and data has been received
        // You can parse the stuff in your instance variable now


    }

}

or in synchronous request try to edit your NSMutableURLRequest

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.f];
YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48
melix
  • 468
  • 4
  • 16
  • thanks for the reply, what's reason for get id result nil, is this not possible with synchronous request ?, thank you – Vicky iOS Jun 06 '15 at 08:52
  • What does NSLog(@"Error getting %@, HTTP status code %li", url, (long)[response statusCode]); print? – melix Jun 06 '15 at 09:00
  • And what does NSError *error = nil; NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; NSLog(@"%@",error); print? – melix Jun 06 '15 at 09:08
  • Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x7f9739383de0 {NSUnderlyingError=0x7f973937d050 "The request timed out." – Vicky iOS Jun 06 '15 at 09:18
  • are you using the simulator? have you checked if the simulator is connected to the internet? (Try with safari) – melix Jun 06 '15 at 09:28
  • yes it is connected, the process of loading data from server is slow by 15 or more sec's – Vicky iOS Jun 06 '15 at 09:33
  • if internet is ok! see the last row of my responce (I edit it) and try it in your code. – melix Jun 06 '15 at 09:34
  • i used the above code of line, it shows the same above error – Vicky iOS Jun 06 '15 at 09:42
  • try iOS Simulator -> Reset Content and Settings; and see http://stackoverflow.com/questions/26972822/error-error-domain-nsurlerrordomain-code-1001-the-request-timed-out – melix Jun 06 '15 at 09:46
  • Nunzio Amazing!! its working i changed timeoutInterval to 120.f, it working perfectly!! thank you for you patience and time. learnt a lot from you – Vicky iOS Jun 06 '15 at 10:05
  • I'm happy for you! You can make a check on my solution ? xD – melix Jun 06 '15 at 10:13