Speed up the response retriever from server in iphone - iphone

I am using following code to get results from server
NSString *queryString = #"MyString"
NSString *response = [NSString stringWithContentsOfURL:[NSURL URLWithString:queryString] encoding:NSUTF8StringEncoding error:&err];
NSLog(#"%#",response);
if (err != nil)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Error"
message: #"An error has occurred. Kindly check your internet connection"
delegate: self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[indicator stopAnimating];
}
else
{
//BLABLA
}
The problem with this code is ,If server shows lag and it takes lets say 3 seconds to get this response
NSString *response = [NSString stringWithContentsOfURL:[NSURL URLWithString:queryString]
For 3 seconds my iPhone screen is jammed. How can i make it run in background so that it won't slow down or jam the mobile
Regards

What you are doing is sending HTTP request from the main thread. that will jam up the UI as you said. You need to spawn a background thread and make a request to your server, when the response comes back then you need to update the UI from the main thread. This is a common pattern in UI coding.
__block__ NSString *response;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
//your server url and request. data comes back in this background thread
response; = [NSString stringWithContentsOfURL:[NSURL URLWithString:queryString] encoding:NSUTF8StringEncoding error:&err];
dispatch_async(dispatch_get_main_queue(), ^{
//update main thread here.
NSLog(#"%#",response);
if (err != nil)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Error"
message: #"An error has occurred."
delegate: self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[indicator stopAnimating];
}
});
});
You can also use performSelectorInBackground:withObject: to spawn a new thread, then the performed selector is responsible for setting up the new thread's autorelease pool, run loop and other configuration details – see "Using NSObject to Spawn a Thread" in Apple's Threading Programming Guide.
You'd probably be better off using Grand Central Dispatch as I posted above. GCD is a newer technology, and is more efficient in terms of memory overhead and lines of code.

You could use ASIHTTPRequest which is my favorite for getting and sending information.

Related

dismissViewControllerAnimated:completion: has a couple second delay

dismissViewControllerAnimated:completion: is working fine in my application, except for the delay between the dismissal.
[api loginWithUsername:[dict objectForKey:#"username"] andPassword:[dict objectForKey:#"password"] andSuccessBlock:^(id json) {
NSLog(#"DONE... %#", [json objectForKey:#"status"]);
NSString *status = [json objectForKey:#"status"];
if([status isEqualToString:#"ok"]){
app.user = [json objectForKey:#"data"];
[self dismissViewControllerAnimated:YES completion:nil];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"could not log you in" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
}];
In my console I can see echo'ed "DONE... ok", meaning the callback block was executed, however about 3-4 seconds later the modal view is finally dismissed.
What could be causing this delay?
If you don't guarantee your UI code is running on Main Thread it might run on some other and in that case, you will experience a few seconds delay!
You can add this to make sure that dismissal is ran on main thread:
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});
Generally, this is not an issue as most of your code will already be running on main thread, since we mostly add code that runs from UIKit methods, like viewDidLoad and such. Those methods are guaranteed to be ran on the main thread.
The issue arises when you end up running code on another thread. One case where that can happen is for example on a completion block invocation of a networking library, where the request is done on background.
check 'viewWillAppear' of the ParentViewController and 'viewWillDisappear' of the currentViewcontroller.
Make sure this two functions does not have any heavy calculations and memory allocations within.
Try changing
NSString *status = [json objectForKey:#"status"];
NSLog(#"DONE... %#", status);
and then try
if([status isEqualToString:#"ok"]){
[self dismissViewControllerAnimated:YES completion:nil];
app.user = [json objectForKey:#"data"];
}else{
As I don't know how big your JSON object is it could be taking a moment to respond and as you're calling it again after the log statement and the doing something else before saying dismiss it could be that.

Game Center multiplayer over 3G/WWAN - getLocalConnectionData timed out

I have a multiplayer game that uses GameCenter for networking. Network games over wifi using GKMatch work perfectly, but over 3G they never connect. My
-[GKMatchmaker findMatchForRequest:
withCompletionHandler:]
completion handler block is invoked with an error code 503, which isn't a GKErrorDomainCode according to that header. Instead it appears to be an HTTP error code.
Here's my code:
//GKLocalPlayer is already authenticated at this point
_matchRequest = [[[GKMatchRequest alloc] init] autorelease];
[_matchRequest setMinPlayers: 2];
[_matchRequest setMaxPlayers: 2];
GKMatchmaker *matchmaker = [GKMatchmaker sharedMatchmaker];
[matchmaker findMatchForRequest: _matchRequest
withCompletionHandler:
^(GKMatch *match, NSError *error) {
if (error)
{
if ([error code] != GKErrorCancelled)
{
dispatch_async(dispatch_get_main_queue(), ^{
[[[[UIAlertView alloc] initWithTitle:
NSLocalizedString(#"Can't find match.", #"Alert title for when automatching failed")
message: [error localizedDescription]
delegate: nil
cancelButtonTitle: NSLocalizedString(#"OK", #"Button text for OK button")
otherButtonTitles: nil] autorelease] show];
});
}
else
{
NSLog(#"Canceled :(");
}
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
// do some main-thread stuff specific to my app
_match = [match retain];
[_match setDelegate: self];
});
}
} ];
}
SNJGKLocalPlayerManager just logs the player into GameCenter. Typical output from this would be a UIAlertView saying "The operation could not be completed. getLocalConnectionData timed out" from the line in the first dispatch_async block. If I use NSLog to output the error code, it's 503.
If you've gotten GameCenter multiplayer to work over 3G and don't want to pore over my code, feel free to share your working code and I'll try to find where mine goes wrong! Thanks!

Incrementing `static int` causes SIGSEGV SEGV_ACCERR

I'm debugging a crash reported as:
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR
The crash is happening on the line that does numberOfFails++.
The app uses ASIHTTP. I personally much prefer using NSURLConnection. I'd never automatically repeat a request for NSURLConnection if it failed because I've never seen it fail when it shouldn't. I'd rather just give the UI a refresh button or show a UIAlertView with a button to try again or something like that.
Anyway, to cooperate with other team members, I'm looking to fix this issue without replacing ASIHTTP with NSURLConnection for now.
The request is being started with something like:
- (void)getResources:(CLLocation *)location withQuery:(NSString *)query {
NSURL *url = [NSURL URLWithString:[NSString stringWithString:#"https://example.com/"]];
self.resourcesAPIRequest = [ASIFormDataRequest requestWithURL:url];
[resourcesAPIRequest setPostValue:[Model instance].oauth_token forKey:#"oauth_token"];
[resourcesAPIRequest setPostValue:[[NSNumber numberWithDouble:location.coordinate.latitude] stringValue] forKey:#"latitude"];
[resourcesAPIRequest setPostValue:[[NSNumber numberWithDouble:location.coordinate.longitude] stringValue] forKey:#"longitude"];
[resourcesAPIRequest setPostValue:query forKey:#"query"];
[resourcesAPIRequest setDelegate:self];
[resourcesAPIRequest setDidFinishSelector:#selector(resourcesAPIReturned:)];
resourcesAPIRequest.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:NSStringFromSelector(#selector(getResources:withQuery:)), #"repeatSelector", location, #"argument1", query, #"argument2", nil];
[resourcesAPIRequest startAsynchronous];
}
One thing I noticed is : <ASIHTTPRequestDelegate> is not in the header file, but this callback method is still being called OK:
#define maximumNumberOfFails 50
- (void)requestFailed:(ASIFormDataRequest *)request {
static int numberOfFails = 0;
if (numberOfFails < maximumNumberOfFails) {
[NSThread sleepForTimeInterval:sleepTimeInSeconds];
if ([request.userInfo objectForKey:#"argument2"]) {
[self performSelector:NSSelectorFromString([request.userInfo objectForKey:#"repeatSelector"]) withObject:[request.userInfo objectForKey:#"argument1"] withObject:[request.userInfo objectForKey:#"argument2"]];
} else if ([request.userInfo objectForKey:#"argument1"]) {
[self performSelector:NSSelectorFromString([request.userInfo objectForKey:#"repeatSelector"]) withObject:[request.userInfo objectForKey:#"argument1"]];
} else {
[self performSelector:NSSelectorFromString([request.userInfo objectForKey:#"repeatSelector"])];
}
numberOfFails++;
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Problem" message:#"There was a problem connecting to the servers. Please make sure you have an Internet connection." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
numberOfFails = 0;
}
}
Also, I think that static int numberOfFails should be static NSUInteger numberOfFails. And, I noticed that the request is started with startAsynchronous. Is static int numberOfFails atomic? That may be why we're getting the error SEGV_ACCERR (Invalid permissions for mapped object).
Thoughts?
The problem likely has nothing to do with your static variable.
Does requestFailed: execute on the main thread, or in a background thread?
If it's on a background thread, you'll need to use performSelectorOnMainThread:withObject:.
If it's on the main thread, you may need to take a pass through the runloop before executing a new HTTP request. To do that, use performSelector:withObject:afterDelay:, and pass '0.0' as the delay.
You'll notice that both those methods allow only a single method parameter. Typically, you pass your parameters in a NSDictionary rather than try to parse out the number of parameters beforehand, like you're doing.

using NSURLConnection instead of stringWithContentsOfURL

Guys, I was retrieving an XML response from a .php script on my server using the following code:
NSString *xmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]
encoding:NSUTF8StringEncoding error:&error];
and
//tried also: if(error)
if(!xmlString)
{
NSString * errorString = [NSString stringWithFormat:#"Unable to download xml data (Error code %i )", [error code]];
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:#"Error loading content" message:errorString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
//*** now I have a touchXML xPath query code that extracts what I need from XML
My .php script wasn't working today and my iApp was freezing without any alerts, errors or notifications? I thought that the code above will handle errors but it doesn't???
A) Then what type of error does it catch?
Then I remembered that i I should try working with NSURLConnection and its delegate methods to catch any error that occurs.
//Inside viewDidLoad method
NSMutableData *responseData = [[NSMutableData alloc] init];
NSURL *baseURL = [[NSURL URLWithString:self.chosenDrawRss] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
NSString *xmlString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
The problem I had using this approach was that app ran the Connection in the background and continued executing the rest of the code found in viewDidLoad, so I had to reorganize my code a bit by moving it to another method that I call from delegate method connectionDidFinishLoading. The problem I have is that my delegate methods are not called. Well, all except didFailWithError: if I try to load URL that doesn't exist.
UPDATE:
delegate methods are called but it takes one minute or so for delegate method to be called and until alert message pops out...
B) Could I use stringWithContentsOfURL and still have an alert to the user if anything happens?
C) If not, then I need help with setting up NSURLConnection approach? I mean, what I'm missing here is why aren't my delegate methods called?
I truly hope my questions make sense :D
L
If your delegate method is called in didFailWithError then i suppose it will even be called in
connectionDidFinishLoading. Check by enabling a breakpoint in the delegate methods and track the flow. It may have happened the function returns before you call the delegate.
NSString *xmlString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]
should be called in your delegate connectionDidFinishLoading method and in
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
I dont see why the alert should pop up after a minute unless you are doing some processing before displaying it.

What should be the reason behind this leaky line?

I have follwowing peace of code in which I have specified the leaky line . As I am new to iPhone developement I can't understand what actually is wrong with that line . please have a look at that line and tell me .
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
//take away
//NSURL *url1 = [[NSURL alloc] initWithString:#"http://url/Service.asmx/takeAwayList"];
NSURL *url1 = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"%#/Service.asmx/takeAwayList",serviceURL]];
NSMutableURLRequest* request1=[NSMutableURLRequest requestWithURL:url1];
[request1 setHTTPMethod:#"POST"];
[request1 setTimeoutInterval:10];
//*****the leaky line***********************///
NSData *data2=[[NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil] autorelease];
if(data2 == nil)
{
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"The network is not available.\n Please check the Internet connection." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
NSXMLParser *xmlParser1 = [[NSXMLParser alloc] initWithData:data2];
//Initialize the delegate.
TakeAwayParser *takeAwayParser = [[TakeAwayParser alloc] initTakeAwayParser];
//Set delegate
[xmlParser1 setDelegate:takeAwayParser];
//Start parsing the XML file.
#try {
BOOL success = [xmlParser1 parse];
if(success)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
}
#catch (NSException * e) {
NSLog(#"Exception in parsing %# %#",[e name], [e reason]);
}
[takeAwayParser release];
[xmlParser1 release];
}
//[request1 release];
// [response1 release];
//
[url1 release];
// [data2 release];
//new arrivals
//[data2 release];
[pool release];
I had issues with this as well in my Large project. After working with an Apple engineer on trying to locate the leaks, he finally asked the main Apple dev team behind NSURLConnection. They basically said that there is an internal cache that is not clearable at all in NSURLConnection and it was a known issue.
So I set out looking for alternatives. I found ASIHTTPConnection (link below) which works off of CFNetwork. It is designed to be a drop-in replacement for NSURLConnection, plus a bunch of other awesome goodies like downloading to disk instead of memory, download resuming, progress bar callbacks etc..
I have used it in all my projects and have never had any issues or complaints. An, in answer to your question, this is how I got rid of those memory leaks.
http://allseeing-i.com/ASIHTTPRequest/
This line isn't leaking, you shouldn't even being autoreleasing it.
Do yourself a favor and read the Memory Management Guide in Apple's developer docs, commented out releases in your code do not bode well.
EDIT: Hrm I take that back your code is completely fine except for that one line. Are you sure that it's leaking? It's returning an object with a retain count of 0, so you autoreleasing it should be causing trouble because it already has a retain count of 0.