scheduledTimerWithTimeInterval vs performselector with delay with iOS 5.0
Asked Answered
H

2

10

i am doing function call with scheduledTimerWithTimeInterval. i am just checking that xml parsing is completed or not for particular web services and invalidating timer in didEndElement method after getting successful response.

timerForStopWebService = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(stopWS) userInfo:nil repeats:NO];

now i am facing problem with iOS 5.0 and its working fine in other iOS versions. in iOS 5.0, a function stopWS call anytime even if i am invalidating it. let me know if you have solution for that.

and now i am implementing performselector with delay and set boolean variables in stopWS to identify that parsing is completed or not. i just want to know that is there any major difference between this? and does this solution works for my problem?

if other way exists, please suggest me, thanks.

Hartle answered 14/11, 2011 at 9:30 Comment(4)
I don't understand what you are looking for. Do you want the method to be ALWAYS called ? Or do you want to be able to invalidate it so it is not called ? That's not clear.Pu
i just want to know difference between scheduledTimerWithTimeInterval vs performselector with delay in depth because i am facing problem with iOS 5.0.Hartle
And what is this problem ? You talk about it in your question but it's not clear.Pu
i am doing scheduledTimerWithTimeInterval to call one method. now problem is with iOS 5.0, the method is calling repetitively even if i am invalidating timer.Hartle
K
21

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?

Keener answered 23/11, 2011 at 11:4 Comment(0)
S
0

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.

Sardinian answered 18/11, 2011 at 3:57 Comment(2)
You don't need to store the timer reference. Once it's added to the loop you only need the reference if you want to cancel it... same as cancelPerformSelector. Otherwise, you could make a timer with a single firing.Throe
The main difference is when you want to cancel it, which the asker seemed to need. In cancelPerformSelector you only need the reference to the object, which you usually have access to anyways (probably it is just self). To cancel a timer, you must have stored the timer somewhere, probably as an instance variable.Sardinian

© 2022 - 2024 — McMap. All rights reserved.