Reputation: 2801
Hey I have an application I am working on and frequently I will get the following message:
NSURLConnection error: Error Domain=NSURLErrorDomain Code=-1005
"The network connection was lost."
My log will log this and than my application will freeze. My hope is, what is my best option to just send a notification that internet was lost and please try again rather than it closing my application?
Here is what I am doing:
User logins -> Main controller loads and runs this:
// EXECUTE SERVER CALL
-(void)makeRequests
{
/* GRAB USERNAME TO BE SENT OVER FOR POPULATING DATA */
NSArray *get = [[SSKeychain allAccounts] init];
NSString *username = [get[0] objectForKey:@"acct"];
NSDictionary *dictionary = @{@"function": @"populateHomePage", @"username" : username};
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (error)
NSLog(@"%s: JSON encode error: %@", __FUNCTION__, error);
NSURL *url = [NSURL URLWithString:@"/IOS-Frame/grab-data.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *params = [NSString stringWithFormat:@"json=%@",
[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *paramsData = [params dataUsingEncoding:NSUTF8StringEncoding];
[request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)[paramsData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:paramsData];
// issue the request
NSURLResponse *response = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error)
NSLog(@"%s: NSURLConnection error: %@", __FUNCTION__, error);
NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"responseString: %@",responseString);
// GRAB STATUS OBJECT
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:returnData //1
options:kNilOptions
error:&error];
self.dataGrabed_dictionary = [json objectForKey:@"retrieved_data"];
}
Upvotes: 0
Views: 135
Reputation: 505
Follow this answer for internet connection test and just order methods in queue.
- (void)testInternetConnection
{
internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
// Internet is reachable
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Yayyy, we have the interwebs!");
//show activity indicator........block ui until data loads.
//queue methods
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Save in the background
[self makerequests];
dispatch_async(dispatch_get_main_queue(), ^{
// Perform UI functions on the main thread!
//dismiss activity indicator
});
});
});
};
// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Someone broke the internet :(");
//show some alert....
});
};
[internetReachableFoo startNotifier];
}
P.S. This is hint, not an exact answer.
Upvotes: 2
Reputation: 6396
I would wrap everything after the if(error)
condition in an else
statement (no sense in setting your response/json if the service failed). This will make sure that you're not trying to create objects with nil
data that could cause issues. In your if
statement that catches the error, you might also take this chance to let the calling method know that the service failed.
It's possible that the calling method is waiting for data to come back from this service. If it fails, the data will never come.
It's unclear whether or not this is being called on the main thread or not, as the calling method isn't shown. That being said, you really want to make sure that you hit web services from a background thread in order to keep your UI from freezing while it runs. I personally prefer dispatch_async
but you can look into different ways of sending tasks to background threads.
Upvotes: 1