How to abort a synchronized URL request in IPhone - iphone

I have an API call. It takes more than 2 minute to get a response from the server. how can i abort it manually while it is processing?
Here is my code:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setTimeoutInterval: 600.0];
[request setHTTPMethod:#"GET"];
NSMutableDictionary* headers = [[[NSMutableDictionary alloc] init] autorelease];
[headers setValue:#"application/json" forKey:#"Content-Type"];
[headers setValue:#"application/json" forKey:#"Accept"];
[headers setValue:val forKey:#"Authorization"];
[request setAllHTTPHeaderFields:headers];
NSError *Error;
NSURLResponse *response = [[NSURLResponse alloc] init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&Error];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
how can i abort the request while its processing.
Thanks

You can't. Use asynchronous connections if you want to be able to cancel.
Also, BTW, you have some errors in your code:
NSError *Error;
NSURLResponse *response = [[NSURLResponse alloc] init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&Error];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
For good measure, Error should be initialized to nil.
You are leaking response. Initialize it to nil, and sendSynchronousRequest:returningResponse:error: will fill it in (if the request succeeds, anyway).
The line [[NSURLConnection alloc] initWithRequest:request delegate:self] starts an asynchronous connection and releases it, after you already did a synchronous connection for the same request. And it leaks the NSURLConnection object. Did you even implement the necessary methods for the informal delegate protocol on your self object?

You can't, as the thread is being blocked by your synchronous request.
synchronous load is built on top of the asynchronous loading code made available by the class. The calling thread is blocked while the asynchronous (sic) loading system performs the URL load on a thread spawned specifically for this load request.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html

Related

How to periodically poll data from server on a background thread?

I have a chat like app, and to receive periodic chats, I want to poll server at regular intervals , say 5 mins..and check if there are any new messages.
As a beginner, I have tried simple NSURLConnection and send and receive data, but stuck while polling.
What is a correct way of polling? Should I use NSTimer to poll regularly?
Is there any sample code that can help me?
I have a timer running on main thread which invokes a background thread every 5 seconds and sends request to server. Now what happening is, the delegates for NSURLConnection are not getting called. What can be done?
Please help.
Thanks
You can launch an NSTimer on a background thread and make calls to the main thread periodically, this is how I do it:
[self performSelectorOnMainThread:#selector(LaunchTimer) withObject:nil waitUntilDone:NO];
Just make a "LaunchTimer" function that calls an update function at a certain interval, which makes a call to NSURLConnection, and when you receive the new messages, update the chat window by making a call to the main thread like so:
[self performSelectorOnMainThread:#selector(update:) withObject:newMessages waitUntilDone:NO];
Continuing from answer of Patrick :- If you can try NSURLConnection without delegate,
-(void)update:(id)objmessage
{
NSString *post =[NSString stringWithFormat:#"timestamp=%#&user_id=%#",DatetimeString,UID];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"Your server URL"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *returnData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
returnString=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
if(!returnString)
{
UIAlertView *erroralert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"There is an error getting response" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[erroralert show];
[erroralert release];
}
else
{
NSLog(#"%#",returnString);
}
SBJSON *json = [[SBJSON new] autorelease];
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[json objectWithString:returnString error:nil]];
//NSLog(#"%#",dic);
}
Solved this with following method:
[self performSelectorOnMainThread:#selector(performPollinginBackground) withObject:nil waitUntilDone:NO];
-(void)performPollinginBackground{
//call server task here
}

How can I use NSURLConnection to make connection in background

In one of my previous post someone suggest me to use the
NSURLConnection *conn = [NSURLConnection connectionWithRequest:theRequest delegate:self];
To make connection in a new thread to keep GUI working without freezing during the connection.
But It now I can realize that this doesn't work.
This is the whole method.
- (void)postRequestWithURLState:(NSString *)url
{
NSString *bodyRequest = nil;
NSURL *requestURL = [NSURL URLWithString:url];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
//NSLog(#"-------------- bodyRequest: %#", bodyRequest);
[theRequest setURL:requestURL];
[theRequest setTimeoutInterval:2.0];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody:[bodyRequest dataUsingEncoding:NSASCIIStringEncoding]];
[self.oauthAuthentication authorizeRequest:theRequest];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:theRequest delegate:self];
self.web = conn;
}
That is part of a class called greader. So I use this method in this mode.
reader = [[greader alloc] init];
[reader setDelegate:self];
Than I call it [reader postRequestWithURLState:url].
But in this way it still freeze my app for some seconds. How can I execute this action without freezing my app for some seconds?
This is strange. I cannot see why this method would freeze your UI.
One simple solution is to call the method in separate thread. Make the reader a property (or pass it to the method below):
-(void)postReaderRequest:(NSURL)url {
[self.reader postRequestWithURLState:url];
}
and then
[self performSelectorInBackground:#selector(postReaderRequest)
withObject:[NSArray arrayWithObject:url]];
If the result is the same, it must have been something else.
Try using NSThread to execute your NSURLConnection

How to send login and password to a server using ASIHTTPRequest?

I'm pretty new to everything that deals with http and stuff like that and i know almost nothing about how http methods work so i need your advice. In my app i have to send login and password to a server and get a response. By now i'm trying to write a method (it's called STUB in English i believe) which will work in a general way without communicating to actual server. Should i use POST method in a manner similar to:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:some_server_url];
[request addPostValue:passwordString forKey:loginString];
or something else? if first how do i receive the response from the server? Could you please show me few lines of code?
You can pass login details to server using HTTP POST method like this :
-(NSDictionary*)userLogIn:(NSMutableDictionary*)inputDictionary{
SBJSON *parser = [[SBJSON alloc] init];
NSString *convertedString=[NSString stringWithFormat:#"login=%#&password=%#",username, password];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"Give the url here"]];
[request setHTTPBody:[convertedString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"XXXX" forHTTPHeaderField:#"USER_AGENT"];
[request setHTTPMethod:#"POST"];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];
NSDictionary *statuses = [parser objectWithString:json_string error:nil];
[parser release];
[json_string release];
return statuses;
}
This is a sample code and you have to change according to your requirements.

iPhone Objective-C: combining methods together

I'm a complete noob at objective-C, so this might be a very silly question to a lot of you.
Currently, I have a view with a 'SignIn' button, which when clicked, activates a sigupUp IBAction that I have defined below. Basically, this method needs to make a JSON call and get back some data about the user.
So, right now, my code looks something like this:
-(IBAction)signIn:(id)sender{
//run registration API call
//establish connection
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.apicallhere.com/api/auth"]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Current-Type"];
[request setHTTPBody:postData];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
responseData = [[NSMutableData data] retain];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{[responseData setLength:0];}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{[responseData appendData:data];}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{/*NSLog(#"%#",error);*/}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *response=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
....code carries on from here.
}
As you can see, the problem with my code is that even though it is initiated via the 'SignUp' method, it finishes with the 'connectionDidFinishLoading' method. I want to have all this code in a single method. Not 2 separate ones, as I want to be able to return a boolean verifying if the connection was successful or not.
If someone could please tell me how to code up this procedure into a single method, I would really appreciate it.
If you really want the code all in one method, you're talking about potentially blocking all of the UI and so forth on a synchronous HTTP request method and response call.
The method to put this all "inline" is sendSynchronousRequest:returningResponse:error on NSURLConnection
[NSURLConnection sendSynchronousRequest:returningResponse:error:]
i.e.
NSError *error;
NSURLResponse *response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
... do something with the NSURLResponse to enjoy with your data appropriately...
I would personally encourage you to look at an alternative for most of this sort of thing towards the asynchronous methods.

Is the ASIHTTPRequest is not asynchronous?

I am using ASIHTTPRequest to fetch some data from a web service.
I am making requests using a loop.
The problem is that it doesn't seem the request is going asynchronously so that my activityindicator is not working .
Is it true that ASIHTTPRequest is not asynchronous .
or should i use the regular nsmutablerequest to perform asynchronous request .
You should put your request in a download queue, i.e.
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:request];
[request release];
Just
[request startAsynchronous];
runs the request on the UI thread, so you should try it with download queue.
For Sync Synchronous
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
}
Creating an asynchronous request
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
And there is lot of more options like Queue and many more
You can refer http://allseeing-i.com/ASIHTTPRequest/How-to-use