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