对performSelector:withObject:afterDelay:inModes的保留计数的影响 [英] the effect on retain count of performSelector:withObject:afterDelay:inModes

查看:416
本文介绍了对performSelector:withObject:afterDelay:inModes的保留计数的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的程序:

-(void)doSomething {

 NSLog(@"self rc=%d", [self retainCount]);

 [self performSelector:@selector(doMe:) withObject:nil afterDelay:0 inModes:[NSArray arrayWithObject:NSDefaultRunLoopMode]];

 NSLog(@"self rc=%d", [self retainCount]);
}

-(void)doMe:(id)object {

 NSLog(@"i'm done");

 NSLog(@"self rc=%d", [self retainCount]);

}

输出:

self rc=1

self rc=2

i'm done

self rc=2

为什么保留计数增加到2并保持不变?

Why does the retain count increment to and stay at 2?

推荐答案

来自 2013/11/01答案

文档似乎已按预期进行了更新,现在他们没有说保留了目标对象,但是仍然在运行循环中提到了计划的计时器.

The docs appear to be updated, as expected, now they don't say the target object is retained however they still mention an scheduled timer in the runloop.

如果使用NSTimer,则该对象必须由某人保留,否则将无法保证可以执行选择器,因为没有人可以确保该对象仍然存在.如果不使用ARC的weak,它将崩溃. 我认为,Apple编辑了其文档以不提及实现细节,尽管我认为这很明显.

If an NSTimer is used then the object must be retained by somebody or there would be no guarantee the selector can be performed since no-one could assure the object would be still alive. If not using ARC's weak it will crash. In my opinion, Apple edited its documentation to not mention implementation details, although I think it is pretty obvious.

以上文档根本没有提及 retain 一词,但这并不意味着未保留目标.

Above document does NOT mention the word retain at all however, it does NOT mean the target is is not retained.

这是我在模拟器iOS7.0中的调试器中尝试过的操作:

This is what I tried in the debugger in the simulator iOS7.0:

(lldb) p (int)[self retainCount]
(int) $1 = 8
(lldb) expr (void)[self performSelector:@selector(description) withObject:nil afterDelay:1.0]
(lldb) p (int)[self retainCount]
(int) $2 = 9
(lldb) expr (void)[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(description) object:nil]
(lldb) p (int)[self retainCount]
(int) $3 = 8

结论:对象已保留,但我们不应该在意. 先生解决了:p

Conclusion: the object is retained, but it is not something we should care about. Mister solved :p

上一个答案:(仍然有效)

此方法保留接收者和 anArgument参数,直到之后 选择器已执行.

This method retains the receiver and the anArgument parameter until after the selector is performed.

因为如果未保留该对象,则该对象可能在执行前被释放,并且您的应用程序将崩溃.

Because if the object is not retained, the object might be released before it gets executed and your app will crash.

设置和尝试使用NSTimers触发方法时,逻辑是相同的.如果目标对象已不存在(已释放),则实际上触发计时器时,您的应用程序将崩溃.因此,performSelector:withObject:afterDelay:...及其朋友在这里使我们的生活更轻松一点,因为它可以确保在触发计时器时该应用程序不会崩溃;)

Is the same logic when you set and try to fire a method using NSTimers. When the timer is actually fired if the target object does not exist anymore (is released) your app will crash. So performSelector:withObject:afterDelay:... and its friends are here to make our life a little bit easier because it makes sure the app won't crash when the timer is fired ;)

希望有帮助

这篇关于对performSelector:withObject:afterDelay:inModes的保留计数的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆