scheduledTimerWithTimeInterval vs performselector with delay with iOS 5.0

筅森魡賤 提交于 2019-12-04 03:14:29
Abhinit

Here are your differences

performSelectorWithObjectAfterDelay

  • as the name suggests performs a selector after a specified number of seconds. ONCE.

  • The care that you need to take here is that you need to cancel any previous perform requests before the object that the selector is being performed on is released. For that use the cancelPerformSelector method.

scheduledTimerWithTimeInterval

  • this method gives you the ability to call a selector after a specified duration too but it also has a parameter [repeats:] that lets you call the same selector REPEATEDLY

  • You can also pass in invocations to call selectors, which are specially helpful when your selector needs a lot of arguments.

  • You need to invalidate the timer when its no longer needed. This should do the trick

    [myTimer invalidate]; myTimer = nil;

Also this is the most definitive thread on NSTimer, please have a look at it. How do I use NSTimer?

zeroimpl

You can use performSelectorWithObjectAfterDelay and then cancelPerformSelector to abort it if no longer needed. I think this is easier than scheduledTimerWithTimeInterval since you don't need to store a reference to the timer. For the most part these two approaches should behave the same though.

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