performSelector:withObject: and its retain behavior

三世轮回 提交于 2019-11-30 09:11:55
newacct

You are looking at the wrong function in the documentation.

Retain

performSelector:withObject:afterDelay: and similar functions (with afterDelay) retain the receiver and arguments, because the execute later

No Retain

performSelector:withObject: and similar functions (without afterDelay) do not retain anything, since they just call the function directly.

[[self delegate] performSelector:@selector(tryToSendStoreData:) withObject:userData];

does the exact same thing as

[[self delegate] tryToSendStoreData:userData];

While @newacct gave the correct answer, but it was not for the question that @Flex_Addicted had asked, i.e. citations from Apple's documentation that the observed behaviour is indeed guranteed. Below is a (partial) citation, but we'll have to go through a couple of hoops to get there -

The documentation for performSelector:withObject:afterDelay: states that

This method sets up a timer to perform the aSelector message on the current thread’s run loop.

so next we head over to the documentation for NSRunLoop and there we find that only one method exists that allows the capability to enqueue stuff on the run loop -
performSelector:target:argument:order:modes:, whose documentation states that

This method sets up a timer to perform the aSelector message on the current thread’s run loop at the start of the next run loop iteration. The timer is configured to run in the modes specified by the modes parameter...The receiver retains the target and anArgument objects until the timer for the selector fires, and then releases them as part of its cleanup.

Of course, nothing guarantees that [NSObject performSelector:withObject:afterDelay:] always uses [NSRunLoop performSelector:target:argument:order:modes:] (although this answer would be complete if someone could come up with documentation for that), but at least this is a step towards the mystery of answering the riddles the holy scriptures riddle us with.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!