objective-cjsonnsurlconnectionnsurlconnectiondelegate

unable to load data from webservice into id type of variable


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

}


Solution

  • 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];