Trade‑off Between +load and +initialize for Deterministic Objective‑C Initialization
18.5K reputation · 27 Sept 2021, 21:05 UTC
The goal is to obtain a repeatable, predictable initialization sequence for Objective‑C classes across multiple builds, ensuring that dependent frameworks are set up in the same order every time.
Both +load and +initialize are invoked by the runtime, but their invocation order among unrelated classes or categories is unspecified and can shift with linking order or loader state. Mixing these methods with explicit init functions (e.g., constructor attributes or calls from main()) risks double‑initialization or missed calls if the runtime still triggers the methods. This uncertainty makes it difficult to guarantee deterministic startup without additional guarding.
Which approach—relying solely on +load, relying solely on +initialize, or replacing both with an explicit init function—provides the most reliable deterministic order while avoiding double‑initialization? How can developers verify that the chosen method yields consistent ordering across different linker configurations and build environments? What safeguards are necessary to prevent unintended multiple invocations when combining runtime‑triggered methods with explicit initialization?