objective c - What happens when my method takes too long to complete when using a timer in obj-c? -
i have method in iphone project called upon nstimer every 0.05 seconds. created timer using:
nstimer mytimer = [nstimer scheduledtimerwithtimeinterval:0.05 target:self selector:@selector(mymethod) userinfo:nil repeats:yes]; the method set most. here outline:
-(void)mymethod { //lots of processing } now here's issue. quite possible method's execution time greater 0.05 seconds (the interval calling method). happen if execution of method equal or greater of interval 0.05? sec cpu thread opened, or programme crash?
in these cases, advise test.
i have created simple 1 view ios app, , changed toe viewcontroller class following:
// // viewcontroller.m // test // #import "viewcontroller.h" @interface viewcontroller () @property nstimer *mytimer; @end @implementation viewcontroller - (void) mymethod { nslog(@"entered testclass"); sleep(3); nslog(@"exited testclass"); } - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; self.mytimer = [nstimer scheduledtimerwithtimeinterval:1.0 target:self selector:@selector(mymethod) userinfo:nil repeats:yes]; } - (void)viewwilldisappear:(bool)animated { [self.mytimer invalidate]; [super viewwilldisappear:animated]; } @end the console listing of test shows us:
2014-06-19 08:30:56.628 test[589:60b] entered testclass 2014-06-19 08:30:59.632 test[589:60b] exited testclass 2014-06-19 08:31:00.627 test[589:60b] entered testclass 2014-06-19 08:31:03.628 test[589:60b] exited testclass 2014-06-19 08:31:04.627 test[589:60b] entered testclass 2014-06-19 08:31:07.628 test[589:60b] exited testclass 2014-06-19 08:31:08.627 test[589:60b] entered testclass 2014-06-19 08:31:11.628 test[589:60b] exited testclass conclusion: since not launching mymethod in parallel asynchronous thread, routine starts after previous run has been terminated. , timer takes it's time (in case 1 second) relaunch mymethod.
happy programming !
objective-c nstimer
No comments:
Post a Comment