1
votes

I have a TableView that recieves data from a server with a method call retrieveData. I use the Reachability to test if the user has internet connection. If YES the retrieveData is called. If NOT i get a NSLog printed.

All works fine, BUT.. Even if it has connection, the table takes a few second to load and that's not what I want. How can I be immediately?

I check the connection in the viewDidLoad method.

- (void)viewDidLoad
{
    [super viewDidLoad];

    Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];

    reach.reachableBlock = ^(Reachability*reach)
    {
        // Load the Table Content
        [self retrieveData];
    };

    reach.unreachableBlock = ^(Reachability*reach)
    {
        NSLog(@"no internet");
    };

    [reach startNotifier];

}
4
Seems to be a table view loading issue if Reachability returns immediately. Probe in your code where it takes the time.Ayan Sengupta
Why are you using Reachability at all? That's more for knowing when the user transitions to or from online. If the table view is going to be presented regardless, just do the network connection and deal with the error if it comes back. It sounds like you're actually asking "how can I get network data in zero seconds?"Tommy
@Tommy I just want to check the connection to, if so load the table, if not. send a notification for the user.Diego Rodrigues

4 Answers

1
votes

Even though Google site "most likely" won't go down, In theory it is a bad idea to rely on the reachability of hostname to determine connectivity.

Once you have downloaded and imported Reachbility.m and Reachbility.h files

create a helper function:

-(BOOL)IsConnected{
  Reachability *reachability = [Reachability reachabilityForInternetConnection];
  NetworkStatus networkStatus = [reachability currentReachabilityStatus];

  return !(networkStatus == NotReachable);    
}

Then use it

if([self IsConnected]){
 [self retrieveData];
}
else{
  //not connected to internet!
}
0
votes

Data loading takes time. If you want to show your data immediately you should load it before you show your view controller.

0
votes

Instead of using Reachability inside your view controller where it has to verify connection status while the view is loading, move it to your AppDelegate where it's always tracking connection state. Then when your view controller loads, just check the state of the existing Reachability and take whatever action is appropriate.

To clarify, the delay you're seeing now is Reachability trying to establish a connection to google.com, which takes some finite amount of time. By moving Reachability handling into your app delegate you front load that process so that it happens during the application load process.

In your AppDelegate.h add:

@property (nonatomic, strong, readonly) Reachability* reachability;

In your AppDelegate.m file, in applicationDidFinishLaunching add:

_reachability = [Reachability reachabilityWithHostname:@"www.google.com"];
[_reachability startNotifier];

Then, you can check reachability with:

AppDelegate* appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
if(appDelegate.reachability.isReachable) {
    ...
}
0
votes
-(void)checkNetworkStatus{
    Reachability* internetAvailable = [Reachability reachabilityForInternetConnection];
    NetworkStatus internetStatus = [internetAvailable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:{
            NSLog(@"The internet is down.");
            break;
        }case ReachableViaWiFi:{
            NSLog(@"The internet is working via WIFI.");
            break;
        }case ReachableViaWWAN:{
            NSLog(@"The internet is working via WWAN.");
            break;
        }
    }
}