iOS Data Tracking with CoreData
Article Summary
Emre Havan from Freeletics ditched third-party analytics SDKs and built a custom tracking system using CoreData. The result? Full control over batching, performance, and data flow without external dependencies.
Most iOS apps rely on Firebase or Mixpanel for event tracking, but Freeletics took a different path. This deep dive walks through their production implementation of a custom tracking infrastructure using CoreData, complete with storage, batching, and network layers that handle offline scenarios and thread safety.
Key Takeaways
- Three-layer architecture: storage (CoreData), batcher (coordination), and sender (network)
- Background context with performAndWait ensures thread-safe database operations across queues
- Batch size threshold (20 events) balances real-time tracking with network efficiency
- Protected data checks prevent crashes when device is locked or encrypted
- Custom queue detection avoids main thread violations when checking UIApplication state
Building custom analytics with CoreData gives you complete control over batching logic, offline persistence, and performance without third-party SDK overhead.
About This Article
CoreData isn't thread-safe by default. Freeletics had to make sure all database operations ran synchronously on the same queue to avoid race conditions and data corruption when multiple threads accessed the system.
Emre Havan's team set up a background context using privateQueueConcurrencyType and wrapped all CRUD operations in performAndWait closures. This ensured everything executed on a single queue without conflicts.
The system now handles concurrent access from any thread safely while keeping data intact. Events persist reliably even when the app moves between foreground and background states.