0
我決定在懷疑一些行爲來運行一個測試:ARC - 如何擺脫的東西
在我的視圖控制器我有一個新的遊戲方法,做到這一點:
-(void) newClassicGame {
if (classicGame!=nil) {
[classicGame saveStats];
}
classicGameController = nil;
classicGameController = [[RegularGameViewController alloc] initWithPowerMode:NO ];
classicGame = nil;
classicGame = [[RegularGame alloc] initWithViewController:classicGameController];
classicGameController.game = classicGame; // game property is __weak to avoid
//retain cycles.
[classicGame startGame];
}
,並如果測試對象獲得與ARC破壞,classicGame的startGame方法,我把這個:
-(void) startGame
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"once");
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(repeat) userInfo:nil repeats:YES];
});
//other stuff..
}
-(void) repeat {
NSLog(@"I repeat");
}
對於那些誰不熟悉dispatch_once
方法,它只是可以確保的方法該塊在應用程序的生命週期中只會被調用一次,而不會再被調用。
我開始我的應用程序,打開新遊戲並開始遊戲 - 我看到日誌「我重複」每3秒重複一次「..然後我回到菜單並再次打開新遊戲。奇怪的是,日誌繼續重複,這說明我的classicGame實例未完全破壞,計時器仍在某處運行。任何想法發生了什麼事嗎?
ha!從來沒有想過要尋找NSTimer文檔,所以最後我爲測試創建的定時器失敗了,具有諷刺意味,謝謝 –
如果要檢測何時釋放對象,請使用來自另一個對象的「弱」引用。當指向的對象被釋放時,弱引用被設置爲零。 – Caleb