I started iOS development with iPhoneOS version 3. In that version Core Data was also introduced to mobile platforms. And to be honest, I have really never used anything else for persistence. It may not be perfect, but you have to love it just for automated update callback from NSFetchedResultsController. And who tells that you should use it only to feed tables and collection views? I have used it everywhere where data changes needed to be tracked. Syncing, triggering data refreshes, handling markers on map 🤓
In example model we are showing two types of markers on map. Players and Places. They both have one to one relationship with PlaceOfInterest. PlaceOfInterest entity is the one that is used for showing pins on map.
When ever places or payers are added or removed. Or when players location is updated. These events will trigger create/delete/modify for attached PlaceOfInterest entity.
If we create NSFetchedResultsController for PlaceOfInteres, we will be notified about all these changes through NSFetchedResultsControllerDelegate.
With this logic in place we can easily add additional pin types. Refreshing data somewhere in the background, just updating on it’s own and letting core data feed everything to map. As long as we keep model data in sync, we don’t need to make additional checks for showing new markers and hiding old ones.
With all it’s problems Core Data is still a really neat framework. When ever I need to be kept in loop about data changes, I’ll be using NSFetchedResultsController to track these. No manual data checks for me!
Tags: ios, map, pins, core data, poi