Freeletics Emre Havan Jun 22, 2020

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

Critical Insight

Building custom analytics with CoreData gives you complete control over batching logic, offline persistence, and performance without third-party SDK overhead.

The article reveals a clever DispatchQueue extension that solves a tricky problem with determining the current queue for thread-safe CoreData operations.

About This Article

Problem

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.

Solution

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.

Impact

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.