2012-12-21 110 views
3

我有一個非常輕的ViewController,它在viewDidLoad中什麼都沒做。我將這個視圖放在navigationController的頂部。執行此操作的方法是從塊內部調用的。在showView的調用之後,我添加了一個NSLog,並且該日誌在控制檯中的打印速度非常快,但視圖需要很多加載......我真的不明白可能發生了什麼......任何想法?pushViewController花費太多來顯示視圖

ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) { 
    [self showView]; 
    NSLog(@"EXECUTED");     
}); 

- (void) showView{ 
    TestViewController *test = [[TestViewController alloc]init]; 
    [self.navigationController pushViewController:test animated:NO]; 
} 
+0

你可以發佈'TestViewController'嗎?很難看到這裏發生了什麼 – NSAddict

回答

9

從文檔爲ABAddressBookRequestAccessWithCompletion

完成處理程序被調用上的任意隊列。如果您的應用程序在整個應用程序中使用地址簿,則您有責任確保將該地址簿的所有用法分派到單個隊列,以確保線程安全正常運行。

您應該確保您的UI代碼在主線程上被調用。

ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self showView]; 
     NSLog(@"EXECUTED");     
    }); 
}); 
+0

確保修復你的語法突出顯示,我不能,因爲他們只是空格 – NSAddict

+0

@NSAddict謝謝 - 我通常會注意到這樣的事情。 – rmaddy

+0

謝謝@rmaddy它完美的工作!感謝您的回答! – Andres

1

這可能不是唯一的問題,但根據docs,完成處理傳遞給ABAddressBookRequestAccessWithCompletion被稱爲上的任意隊列。 -showView只能在主隊列上調用,因爲它正在處理UIViewController對象。

要問的另一件事是主隊列上還發生了什麼?是否有其他可能會阻止UI更新的長時間運行的任務?