Initial knowledge of iOS Nstimer Circular reference does not release the issue

Source: Internet
Author: User

Translated from: http://www.codecate.com/code/?p=77

Recently developed encountered Nstimer Target caused circular reference problems, not released, the following is the solution.

A solution on the StackOverflow

Http://stackoverflow.com/questions/16821736/weak-reference-to-nstimer-target-to-prevent-retain-cycle

The original is as follows

Weak Reference to Nstimer Target to Prevent Retain Cycle

I ' m using a nstimer like this:

Timer = [Nstimer scheduledtimerwithtimeinterval:30.0f target:self selector: @selector (tick) Userinfo:nil Repeats:yes];

Of course, Nstimer retains the target which creates a retain cycle. Furthermore, self is ' t a uiviewcontroller so I don ' t has anything like Viewdidunload where I can invalidate the timer to Break the cycle. So I ' m wondering if I could use a weak reference instead:

__weak id weakself = self; Timer = [Nstimer scheduledtimerwithtimeinterval:30.0f target:weakself selector: @selector (tick) Userinfo:nil repeats: YES]; I ' ve heard that the timer must is invalidated (I guess to release it from the Run loop). But we could do, Dealloc, right?-(void) Dealloc {[Timer invalidate];}

Is this a viable option? I ' ve seen a lot of ways so people deal with this issue, but I haven ' t seen this.

Solution Solutions

The proposed code:

__weak id weakself = self; Timer = [Nstimer scheduledtimerwithtimeinterval:30.0f target:weakself selector: @selector (tick) Userinfo:nil repeats: YES];

Have the effect that (i) a weak reference are made to self; (ii) that weak reference was read in order to provide a pointer to Nstimer. It won ' t has the effect of creating an nstimer with a weak reference. The only difference between, that code, and using a __strong reference is, if self is deallocated in between the Es given then your ' ll pass nil to the timer.

The best thing your can do is create a proxy object. Something like:

[...] @implementation Btweaktimertarget {__weak target; SEL selector; }
[...]
-(void) Timerdidfire: (Nstimer *) Timer {if (target) {[target performselector:selector withobject:timer];} else {[Timer I Nvalidate]; }} @end
Then you ' d do something like:
Btweaktimertarget *target = [[Btweaktimertarget alloc] initwithtarget:self selector: @selector (tick)]; Timer = [Nstimer scheduledtimerwithtimeinterval:30.0 target:target selector: @selector (timerdidfire:) ...];

Or even add a class method to Btweaktimertarget of the form +scheduledtimerwithtimeinterval:target:selector: ... to create a neater form of that code. You'll probably want to expose the real nstimer so the can invalidate it, otherwise the rules established would be:

The real target isn ' t retained by the timer;
The timer would fire once after the real target have begun (and probably completed) deallocation, but that firing would be IG Nored and the timer invalidated then.

Resolving code by using the GCD method

– (void) dosomethingrepeatedly
{
Do it once
NSLog (@ "doing something ...");

Repeat it in 2.0 seconds
__weak typeof (self) weakself = self;
Double delayinseconds = 2.0;
dispatch_time_t poptime = Dispatch_time (Dispatch_time_now, (int64_t) (Delayinseconds * nsec_per_sec));
Dispatch_after (Poptime, Dispatch_get_main_queue (), ^ (void) {
[Weakself dosomethingrepeatedly];
});
}

Initial knowledge of iOS Nstimer Circular reference does not release the issue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.