2014-10-17 32 views
4

我在AppDelegate.h文件添加以下代碼:我應該在applicationWillTerminate中刪除觀察者嗎?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(saveDataAndSettings) 
               name:UIApplicationDidEnterBackgroundNotification 
               object:nil]; 
} 

我通常這樣寫:

- (void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

如果我設置的UIViewController類是觀察者(非ARC)。

我想知道,不過,如果我在AppDelegate中做到這一點,我應該還是插入此行:

[[NSNotificationCenter defaultCenter] removeObserver:self]; 

在applicationWillTerminate:方法。

因爲應用程序將被終止,這樣做是否有意義?

在此先感謝

回答

2

在實踐中沒有必要做太多的清理在任何dealloc或應用程序委託的applicationWillTerminate:。應用程序流程即將消失。

像關閉/保存文件和其他類似的清理類似的事情應該完成,但是在即將退出的進程中清理內存或觀察者沒有多大意義。

+0

謝謝你的答案 – ppalancica 2014-10-17 22:56:21

+0

不用擔心,我通常不張貼了很多問題,但是當我這樣做時,我接受很好的答案,而你的確很棒! – ppalancica 2014-10-18 00:18:24

0

如果您使用的是您選擇項目的非ARC的方法,你應該更好地手動解除分配基準像你這樣的通知提及:

- (void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
}