repeating timer with run now
I have some piece of code that I want to run every x seconds but I also want to be able to trigger a run now that will reset the upcoming cycle. I think I would know plenty of ways to implement this (ranging from simple NSTimer to select() style signaling to threading with conditions) but I am looking for the most elegant way - preferably with GCD. Scheduling a block is easy with GCD - but can you also notify that block for immediate execution later on? Just would love to hear opinions on how you would tackle this admittedly easy problem the most elegant way. cheers, Torsten ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: repeating timer with run now
Le 22 août 2013 à 15:36, Torsten Curdt tcu...@vafer.org a écrit : I have some piece of code that I want to run every x seconds but I also want to be able to trigger a run now that will reset the upcoming cycle. I think I would know plenty of ways to implement this (ranging from simple NSTimer to select() style signaling to threading with conditions) but I am looking for the most elegant way - preferably with GCD. Scheduling a block is easy with GCD - but can you also notify that block for immediate execution later on? Just would love to hear opinions on how you would tackle this admittedly easy problem the most elegant way. Why not just keeping a reference on your scheduled block and simply call dispatch_async when you want to execute it immediately ? -- Jean-Daniel ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: repeating timer with run now
Why not just keeping a reference on your scheduled block and simply call dispatch_async when you want to execute it immediately ? It would not re-schedule the timer. So the block could potentially be run twice without much of the desired delay in between. The logic should be something along the lines of def run_now invalidate timer (if there is one running) run block schedule timer to run block in x seconds end Just to explain the desire behaviour. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: repeating timer with run now
Le 22 août 2013 à 16:08, Torsten Curdt tcu...@vafer.org a écrit : Why not just keeping a reference on your scheduled block and simply call dispatch_async when you want to execute it immediately ? It would not re-schedule the timer. So the block could potentially be run twice without much of the desired delay in between. The logic should be something along the lines of def run_now invalidate timer (if there is one running) run block schedule timer to run block in x seconds end Just to explain the desire behaviour. OK, so I think you can just call dispatch_source_set_timer() each time you want to immediately execute your timer and reschedule it, passing 'now' as 'start' argument and your interval as 'interval' argument. -- Jean-Daniel ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com