ios - NSTimer initWithFireDate not working properly -
i use nstimer
periodically download data in background, when wifi enabled. there option press button downloads data manually. therefore possible enable , disable autoupdater. when period "interrupted" diabling autoupdate, want schedule next update when remaining time of period after point of time when enabling over. because nstimer has no pause method, use nstimer's initwithfiredate method.
timer = [[nstimer alloc] initwithfiredate:firedate interval:updatefrequency target:self selector:@selector(timertick:) userinfo:nil repeats:yes]; [[nsrunloop currentrunloop] addtimer:timer formode:nsrunloopcommonmodes];
the firedate
after given date:
nsdate *firedate = [nsdate datewithtimeintervalsincenow:remainingwaitingtime];
despite that,the timer scheduled after timer's initialization. disabling timer , calculating remaining time following way:
nsdate *lastfiredate = [timer firedate]; nsdate *current = [nsdate date]; [timer invalidate]; double timesincelastfire = [current timeintervalsincedate:lastfiredate]; remainingwaitingtime = updatefrequency - timesincelastfire;
the calculated time (remainingwaitingtime) has correct value.
what doing wrong?
best regards, fabian
you have add timer run loop doing following:
[[nsrunloop currentrunloop] addtimer: timer formode: nsdefaultrunloopmode];
Comments
Post a Comment