When I execute this code:
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showButtons) userInfo:nil repeats:NO];
do I need to nil it or release it, ot whatever for memory management?
I am using ARC
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes,
NSTimerwill maintain a strong reference to thetarget, which can cause (especially in repeating timers) strong reference cycles (a.k.a. retain cycles). In your example, though, the timer does not repeat, and is delayed only 0.5, so worst case scenario, you will have a strong reference cycle that will automatically resolve itself in 0.5 seconds.But a common example of an unresolved strong reference cycle would be to have a
UIViewControllerwith aNSTimerproperty that repeats, but because theNSTimerhas a strong reference to theUIViewController, the controller will end up being retained.So, if you’re keeping the
NSTimeras an instance variable, then, yes, you shouldinvalidateit, to resolve the strong reference cycle. If you’re just calling thescheduledTimerWithTimeInterval, but not saving it to an instance variable (as one might infer from your example), then your strong reference cycle will be resolved when theNSTimeris complete.And, by the way, if you’re dealing with repeating
NSTimers, don’t try toinvalidatethem indeallocof the owner of theNSTimerbecause thedeallocobviously will not be called until the strong reference cycle is resolved. In the case of aUIViewController, for example, you might do it inviewDidDisappear.By the way, the Advanced Memory Management Programming Guide explains what strong reference cycles are. Clearly, this is in a section where they’re describing the proper use of weak references, which isn’t applicable here (because you have no control over the fact that
NSTimeruses strong references to the target), but it does explain the concepts of strong reference cycles nicely.If you don’t want your
NSTimerto keep a strong reference toself, in macOS 10.12 and iOS 10, or later, you can use the block rendition and then use theweakSelfpattern:By the way, I notice that you’re calling
showButtons. If you’re trying to just show some controls on your view, you could eliminate the use of theNSTimeraltogether and do something like:This doesn’t suffer the retain issues of
NSTimerobjects, and performs both the delay as well as the graceful showing of the button(s) all in one statement. If you’re doing additional processing in yourshowButtonsmethod, you can put that in thecompletionblock.