How to make a periodic method call in object c?

If the question is not explained, please excuse me. I am developing an iphone Client-Server application, I have created all classes, instances and ect. I can even send get and parse the answer too. In any case, now I need to force my method to be called in a certain period of time (for example, to call it again after 10 seconds). I googled a lot and also take a look at

NSDate , but I could not decide. Now, can someone help me deal with this situation? Thank you very much

+4
source share
4 answers

You can create and schedule an NSTimer instance to call your method for a given time interval. See, in particular, the following convenience method:

 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats 

http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSTimer_Class/Reference/NSTimer.html

+14
source

It’s best to take a peek at Grand Central Dispatch , as you will want to run this in the background:

Use NSTimer mentioned above.

+4
source

You want to use the NSObject -performSelector:withObject:afterDelay: .

Click for documents .

If you have a method that it calls, call it again on yourself, you will have a cyclic polling method that will automatically start with a delay.

I would recommend checking the class variable to see if you really understand it every time, so you can disable it from the outside.

+3
source

Grand Central Dispatch will create another thread to run. therefore, if the timer method (shown below and suggested above) is behind your application, you will need to put the command in a separate thread.

NSTimer is what you should use. for example, if you want to repeat the method that is initiated by the click of a button, you can do this

 - (void)viewDidLoad { [super viewDidLoad]; [cameraControlButtonUp addTarget:self action:@selector(cameraControlButtonUpPressed) forControlEvents:UIControlEventTouchUpInside]; } -(IBAction)buttonDown:(id)sender{ NSInteger tag = [sender tag]; if (tag==1) { buttonCounter=1; timer = [[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(sendJoin) userInfo:nil repeats:YES]retain]; } } -(void)sendJoin { switch (buttonCounter) { case 1: [cClient userDigitalPushAndRelease:372]; break; default: break; } } -(void)cameraControlButtonUpPressed { [timer invalidate]; } 

which will repeat the command until the button is released. keep in mind that you need to associate ibaction with a button event (only a button click event). as well as create a timer in .h and mark button 1 with which you want to use.

for a simpler example; it's pretty simple. just create your method for calling, timer and set the repeat value to YES. then call invalidate to stop it. I had to create a separate sendJoin method because I could not get the numbers in order to pass this method correctly. but if you don’t have any parameters, it’s even easier. just use the timer syntax to create it, then invalidate it when ur did

+3
source

Source: https://habr.com/ru/post/1387796/


All Articles