Re: NSTimer or what?
Re: NSTimer or what?
- Subject: Re: NSTimer or what?
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Tue, 20 Jun 2017 16:49:31 +0700
> On 20 Jun 2017, at 16:24, Alastair Houghton <email@hidden>
> wrote:
>
> On 20 Jun 2017, at 04:04, Gerriet M. Denkmann <email@hidden> wrote:
>>
>> macOS 11+
>>
>> Some Cocoa app which has to do:
>> 1. something a few seconds later
>
> The main issue here isn’t energy use so much as whether you want to be able
> to cancel the operation. If you need to be able to cancel it before it
> fires, you’ll want to use NSTimer or a CFRunLoopTimer.
I indeed need the things to cancel: The app might schedule a thing to be done
in 5 seconds, then decides after 2 seconds that something else has to be done:
so it need to cancel the previous thing.
[NSTimer invalidate] - very simple.
I tried dispatch_after, where the block checks whether it is cancelled before
starting. Works fine. Not the most elegant code though.
I did not try these two solutions, but they seem to be cancellable as well:
NSRunLoop performSelector:target:argument:order:modes:
cancelPerformSelector:target:argument:
NSObject performSelector:withObject:afterDelay:
cancelPreviousPerformRequestsWithTarget:selector:object:
> Energy wise, the fact this is to happen “a few seconds later” would seem to
> imply that it isn’t going to happen often, so it’s probably irrelevant and
> which you choose is a matter of taste.
A valid point. Probably NSTimer is the most elegant solution here.
>
>> 2. some other thing repeatedly about every 0.1 second.
>
> Personally, I’d choose an API that directly supports repeating timers, so I’d
> prefer NSTimer or CFRunLoopTimer over the other options, all else being
> equal. I’d imagine that’s most likely to result in the lowest energy usage
> (repeated use of one-shot timers, it seems to me, is much more likely to
> result in dynamic memory allocation happening every time the timer fires, and
> it’s more complicated in your code because you’ll have to manage your next
> fire time).
I’ll try NSTimer here too and compare the results.
Kind regards,
Gerriet.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden