EDGE FDE
IntermediateMLX Swift in production
Keep on-device Swift inference stable across app lifecycle and newer model families.
Register a custom MLX model architecture in Swift, load weights safely, and gate inference on scenePhase so Metal is not used while backgrounded.
A newer family such as Gemma 4 loads because you registered its model_type. Generation stops when the scene leaves the foreground, so Metal does not die mid-session.
What you'll learn
- A Run in Xcode is not a production proof. MLX uses Metal. iOS takes the GPU away when the scene backgrounds.
- How to set up Xcode 27, add mlx-swift and mlx-swift-lm, and be honest about Mac, Simulator, and device.
- Why a stock loader fails on a new family: the factory keys on
config.jsonmodel_type, not the Hugging Face id. - How to check the type registry, register a missing architecture, then load weights.
- How to observe
scenePhase, cancel generation when the scene is not.active, and resume only in the foreground.
Lessons
Key concepts
- MLX Swift inference runs on Metal. A backgrounded scene can lose the GPU or the process.
- The loader looks up
model_typein a type registry. Gemma 4 and other new families fail if that type is missing. - Register the architecture, then load weights. Do not invent API names. Confirm them in the mlx-swift-lm docs you pinned.
- Observe
scenePhase. Cancel generation when the phase is not.active. Resume only in the foreground.
Takeaways
- You can tell a demo from a shippable MLX Swift path.
- You can set up Xcode 27 and the MLX Swift packages without treating Simulator as a device GPU.
- You can register a missing architecture and load weights without guessing factory names.
- You can stop Metal work when the app backgrounds and start it again only when the scene is active.
Agent briefs
Related courses
Core AI vs Core ML vs MLX · Route work to the right Apple model · Stream tokens and measure speed · PyTorch to Core AI in Xcode · The agent harness (and how to improve it)