views:

60

answers:

1

Hello,

I have an app which got 4 TableViewController which each make an instance of an RSSParser and have a function called commonInit which starts the parsing. Inititally i called the commonInit from the default init function in the TableViewController and it works fine but in case of no internet access I would get 4 alert windows which is kinda bad mojo. So instead I wanted to do a reachability check in the appdelegate and then notify each TableViewController and start the parser. The reachability check is launched in a seperat thread in order to avoid stall if DNS problems etc.

But when I run the app the parsers gets called fine but they never receive any data back. In my parser i have followed the apple example of NSURLConnection for async download of feeds.

So in my appdelegate I have:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 ....
 // Reachability setup, a new thread is created to make it async to avoid possible stall.
 [NSThread detachNewThreadSelector:@selector(checkConnection) toTarget:self withObject: nil];
 ...
 return YES;}

and after this i have:

    -(void)checkConnection{
 //Test for Internet Connection
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

 Reachability *r = [Reachability reachabilityWithHostName:@"www.somehostname.com"];
 NetworkStatus internetStatus = [r currentReachabilityStatus];
 if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
  UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Missing Internet" message:@"Can not connect to the internet at the moment." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
  [errorAlert show];
 } else {
  [[NSNotificationCenter defaultCenter] postNotificationName:@"activation" object:nil];
 }
 [pool release];}

and as you can see I make the following notification if there is a connection:

[[NSNotificationCenter defaultCenter] postNotificationName:@"activation" object:nil];

Hope you guys can help me.

A: 

I think you should post the notification on the main thread.

This is easy with Grand Central Dispatch. It's a bit harder with the old API.

I propose creating a new method:

- (void)postNotification:(NSDictionary *)userInfo {
  NSString *notificationName = [userInfo objectForKey:@"name"];
  [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil];
}

And then replace the line where you are posting the notification now with:

NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"activation" forKey:@"name"];
[self performSelectorOnMainThread:@selector(postNotification:) withObject:userInfo waitUntilDone:NO];
Jongsma