Here i want to implement view actions while running service calls.In my application while getting the service response ,at the same time i need to work simultaneously both action and services response.Because user can not wait until response finished,and i tried lot of ways can you please give me suggestion.It's more helpful for me .Thanks
Asked
Active
Viewed 50 times
0
-
it sounds like your webservice calls arent asynchronous, you'll need to show us some code so we can see what you are doing – Fonix Sep 08 '16 at 06:51
-
@G.P.Reddy.. Use asynchronous webservice requests. – Bharath Sep 08 '16 at 06:52
-
here i am using service call ,like this NSURLResponse *response; NSError *err; NSData *returnData = [NSURLConnection sendSynchronousRequest: theRequest returningResponse:&response error:&err]; – G.P.Reddy Sep 08 '16 at 06:53
-
yep, that is not the function you should be using unless this is already on a background thread. instead use send**Async**hronousRequest, and make sure to handle the response in the completetion handler, and not just below the function call otherwise you will be trying to process the response before it has come back. if all else fails, find a tutorial on asynchronous webservice calls in iOS – Fonix Sep 08 '16 at 06:59
-
have a look [here](http://stackoverflow.com/a/10417181/1219956) – Fonix Sep 08 '16 at 07:09
-
Thank you @Fonix, working :) – G.P.Reddy Sep 08 '16 at 07:17
2 Answers
0
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error)
{
if ([data length] >0 && error == nil)
{
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:data
options:0
error:NULL];
NSString * string = [[NSString alloc] initWithData:data encoding:NSStringEncodingConversionAllowLossy];
NSLog(@"%@ %@",response,string);
}
else if ([data length] == 0 && error == nil)
{
NSLog(@"Nothing.");
}
else if (error != nil){
NSLog(@"Error = %@", error);
}
}];

G.P.Reddy
- 556
- 3
- 20
-1
//simultaniously button click and send request like this NSURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:TimeOutRequest];
//click button programmatically now perfom such action in button touchup event as you want [myButton addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside];

Mr.Javed Multani
- 12,549
- 4
- 53
- 52