dispatch_after(3)
๐ dispatch_after(3) โ the "do it later bestie" manual page ๐
okay so welcome to dispatch_after, a member of the Library Functions Manual family, and honestly? she's that girl. she schedules blocks for deferred execution, which is a fancy way of saying "bestie, not right now, but at a specific vibe-checked moment later." โจ
๐ธ NAME ๐ธ
dispatch_after โ schedule blocks for deferred execution ๐๐
โจ SYNOPSIS โจ
first things first, you gotta bring in the header:
#include <dispatch/dispatch.h>
then here's the two glow-ups available to you:
void
dispatch_after(dispatch_time_t when, dispatch_queue_t queue,
void (^block)(void));
void
dispatch_after_f(dispatch_time_t when, dispatch_queue_t queue,
void *context, void (*function)(void *));
๐ฆ DESCRIPTION ๐ฆ
so what does dispatch_after() actually do? she submits the block to the queue you hand her, at the time specified by the when parameter. that's it, that's the whole job, and she does it flawlessly. ๐
quick note on when: it's a value you create with dispatch_time() or dispatch_walltime(). don't just make up a number, bestie, use the actual functions. ๐ซถ
now, real talk: submission of the block may be delayed by the system in order to improve power consumption and system performance. so if your block shows up a little late, it's not ghosting you, it's the system being โจefficientโจ. the system applies a leeway (peep dispatch_source_set_timer(3)) that is equal to one tenth of the interval between when and the time at which the function is called, and that leeway is capped to at least one millisecond and at most one minute. math! iconic. ๐งฎ๐
and if you want the whole deep dive on submitting blocks to queues, go read dispatch_async(3) โ she's the main character of that topic, fr fr.
๐ CAVEATS ๐
okay listen up because this part matters, no cap:
dispatch_after()retains the passed queue. so the queue is staying alive whether you like it or not. she's not leaving. ๐- specifying
DISPATCH_TIME_NOWas thewhenparameter is supported, but it is not as efficient as just callingdispatch_async(). so like, technically valid, but why would you do that to yourself. ๐ญ - passing
DISPATCH_TIME_FOREVERas thewhenparameter? yeah the result of that is undefined. do not. that's not a vibe, that's a bug waiting to happen. ๐
๐ FUNDAMENTALS ๐
fun fact for your notes app: dispatch_after() is literally just a wrapper around dispatch_after_f(). she's wearing a cute little coat over her more hardcore sibling. the way that she's built different but also built the same. ๐ง
๐ SEE ALSO ๐
go give these queens a read too:
reminder: keep everywhenvalue real, keep your leeway expectations reasonable, and never ever passDISPATCH_TIME_FOREVER. i'm looking at you. ๐
and that's the page, bestie! ๐ this one's brought to you by Darwin, dated May 1, 2009 โ yes she's been serving since 2009 and she's still relevant. periodt. ๐โจ๐ฆ
๐ฆ where this page comes from
- alpine 3.24: libdispatch-doc 6.1.2-r0 ยท Apache-2.0
- arch rolling: libdispatch 6.2.2-1 ยท Apache-2.0
- epel 10: libdispatch 1:6.0.2-1.el10_0 ยท Apache-2.0
- epel 9: libdispatch 1:6.0.2-1.el9 ยท Apache-2.0
- fedora 43: libdispatch 1:6.0.2-3.fc43 ยท Apache-2.0
- fedora 44: libdispatch 1:6.0.2-3.fc43 ยท Apache-2.0
- fedora 44: libdispatch 1:6.0.2-4.fc44 ยท Apache-2.0
- nixos 26.05: swift-corelibs-libdispatch 5.10.1
- opensuse-leap 16.0: libdispatch-devel 5.6.3-bp160.1.1 ยท Apache-2.0
- Copyright (c) 2008-2010 Apple Inc. All rights reserved
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms