How do I implement an Objective-C singleton that is compatible with ARC?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Under ARC, you do not manually retain or release the singleton instance, but you still need to guarantee that only one instance is created. The standard Objective-C solution is a class method backed by dispatch_once, which gives thread-safe, one-time initialization.
A Minimal ARC-Friendly Singleton
Put the shared accessor in the header and make direct initialization unavailable so callers are pushed toward the single instance.
Then implement the singleton in the .m file:
ARC manages the object's lifetime normally. The crucial part is that dispatch_once ensures the block runs exactly one time, even if multiple threads call sharedManager at the same moment.
Why dispatch_once Matters
Without a synchronization mechanism, two threads could both see an uninitialized static variable and create separate instances. dispatch_once solves that race without forcing you to manage locks manually.
It also keeps the code simple. Older singleton examples often override memory-management methods such as retain or release, but those patterns belong to the pre-ARC era and should not be copied into modern code.
Preventing Extra Copies
If your singleton might be copied, implement copyWithZone: and mutableCopyWithZone: so both operations return the same shared instance.
That is not always required, but it is a good defensive step for service-style objects that should never be duplicated.
Using the Singleton Cleanly
A singleton is usually best for cross-cutting services such as configuration, logging, or analytics. Access it from application code through the shared method:
The class should stay focused. If the singleton turns into a bag of unrelated global state, testability and maintainability both degrade quickly.
Testability still matters
Even a correctly implemented singleton can make unit testing harder if too much application state flows through it. When possible, keep the singleton thin and let other objects depend on protocols or explicit collaborators rather than reaching into shared global state from everywhere.
Common Pitfalls
The biggest mistake is overriding allocWithZone: or old retain-cycle methods copied from outdated blog posts. Under ARC, those implementations add noise and often create surprising behavior without solving any real problem.
Another issue is exposing a normal init method while also advertising a shared instance. That gives callers two code paths and makes the singleton rule easy to bypass. Mark init and new unavailable, then use a private initializer internally.
Finally, be careful with mutable state. A singleton is globally reachable, which means it becomes a shared concurrency surface. If multiple threads mutate its properties, you still need normal synchronization or a design that avoids shared writable state.
Summary
- Under ARC, the standard singleton pattern is
dispatch_onceplus a static shared instance. - Mark
initandnewunavailable so consumers use the shared accessor. - Use a private initializer for the actual setup work.
- Return
selffrom copy methods if the object must remain unique. - Keep singleton responsibilities narrow, because global mutable state becomes hard to test and reason about.
Related reading
- How do I import local fonts async?
- How do I improve ASP.NET MVC application performance?
- How do I iterate through two lists in parallel?
- How do I keep track of the time the CPU is used vs the GPUs for deep learning?
- How do I import a Swift file from another Swift file?
- How do I inspect the view hierarchy in iOS?
- How do I know I've hit the threads limit defined in Node?
- How do I limit the number of rows returned by an Oracle query after ordering?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.