2010-07-09 74 views
0

我有一个应用程序,它有4 TableViewController每个做出RSSParser的一个实例,有一个叫做commonInit函数开始解析。 最初我从TableViewController中的默认init函数调用commonInit,它工作正常,但在没有互联网访问的情况下,我会得到4个警告窗口,这是有点不好的mojo。 所以不是我想要做的的appdelegate可达性检查,然后通知每个TableViewController并启动解析器。该可达性检查是在seperat线程启动,以避免失速,如果DNS问题等iPhone线程问题

但是当我运行应用程序的解析器被调用罚款,但他们从来没有收到任何回数据。 在我的解析器中,我遵循NSURLConnection的苹果示例,用于异步下载Feed。

所以在我的appdelegate我:

- (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;} 

,并在此之后,我有:

-(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];} 

,正如你可以看到我做如下通知,如果有一个连接:

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

希望你们能帮助我。

回答

0

我想你应该张贴在主线程的通知。

这是很容易与大中央调度。旧的API有点困难。

我建议创建一个新的方法:

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

,然后替换你在哪里现在张贴通知行:

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