2010-07-09 42 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];