2012-12-11 25 views
0

我試圖調用後添加一些操作的NSXMLParser parserDidStartDocument凍結應用

- (void)parserDidStartDocument:(NSXMLParser *)parser { 
//NSLog(@"found file and started parsing"); 
alertView = [[UIAlertView alloc] initWithTitle:@"Caricamento..." 
             message:@"\n" 
             delegate:self 
          cancelButtonTitle:nil 
          otherButtonTitles:nil]; 

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur 
[alertView addSubview:spinner]; 
[spinner startAnimating]; 
[alertView show]; 

}

但它凍結的應用程序了一會兒,然後,當完成了XML解析,加載AlertView,ECC 。與UIRefreshControl同樣的事情。我滑下tableView,並在解析時凍結應用程序,我不能看到旋轉的旋轉。

有什麼想法?

編輯: 這裏我第一時間致電解析器:

- (void)viewDidAppear:(BOOL)animated { 
[super viewDidAppear:animated]; 

NSString * path = @"thexmlpath.xml"; 
if(!caricato) 
    [NSThread detachNewThreadSelector:@selector(parseXMLFileAtURL:) toTarget:self withObject:path]; 
    //[self parseXMLFileAtURL:path]; 

caricato = YES;} 

在這裏,我打電話的時候,我用的是RefreshControl:

- (void)refreshControlRequest{ 


NSLog(@"refreshing..."); 

NSString * path = @"thexmlpath.xml"; 
[self performSelector:@selector(parseXMLFileAtURL:) withObject:path];} 
+0

我想我必須在後臺添加XML解析任務,所以它不凍結應用程序,但我不知道該怎麼做。 – Wildchild89

+0

顯示你在哪裏調用解析的更多代碼。那就是你需要把它放在後臺隊列中,然後調用更新主隊列上的用戶界面控件的地方。 – Srikanth

+0

現在我添加了performSelector,但應用程序崩潰是由於:EXC_BAD_ACCESS線程錯誤。 – Wildchild89

回答

1

希望這將幫助你

- (void)parserDidStartDocument:(NSXMLParser *)parser { 
//NSLog(@"found file and started parsing"); 
dispatch_queue_t queue = dispatch_get_main_queue(); 
dispatch_async(queue, ^{ 
    alertView = [[UIAlertView alloc] initWithTitle:@"Caricamento..." 
              message:@"\n" 
              delegate:self 
           cancelButtonTitle:nil 
           otherButtonTitles:nil]; 

    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur 
    [alertView addSubview:spinner]; 
    [spinner startAnimating]; 
    [alertView show]; 
}); 
dispatch_release(queue); 


} 
+0

iYaniv您的解決方案是正確的,但除了您提出的建議之外,解析必須移入背景隊列中,然後使用您建議的代碼。因爲現在,解析本身就在主線程上,並且如果你將一些東西分派到主隊列中,它將在主線程上的工作完成後發生。 – Srikanth

+0

如何創建線程並將分析器存儲在其中? – Wildchild89