Synchronizing API service with Core Data

I would like to suggest a future update to the Core Data book include a chapter on synchronizing a web API (remote data) with local storage in Core Data. This is a very common real world use case scenario. There is an excellent chapter / tutorial / talk from @astralbodies from 2016 RWDevCon covering this topic precisely, but it could really use an update and it really should be part of the Core Data book.

In Chapter 3 the Core Data book mentions, “In iOS 9, Apple introduced the ability to specify unique constraints on your Core Data entities. With unique constraints, you can specify in your data model which attributes must always be unique on an entity to avoid adding duplicates.” However this is never mentioned again in the book, nor how to implement it, nor is there ever made mention of NSMergeByPropertyObjectTrumpMergePolicy . It would be useful to have some explanation of how to use this to further simplify the synchronization process.

The proposed chapter could even skip using a “real” API - it could use stubbed JSON data that comes from some fictitious API, and could make use of Codable to convert said JSON to objects that then are synchronized with the local Core Data store. This would simplify the example from RWDevCon 2016 by removing the CouchDB layer.


That’s a great idea. I remember watching that video a while back and found it extremely helpful. Besides the trump merge policy, what other points would you like explanation on that the video did not discuss? I know attribute indexes on managed object data models were not discussed. Let me know! Maybe I might write a post on it!


I’ve passed your suggestion off to the team. Thank you for being a reader and taking an interest in our book!

Managing Editor

This topic was automatically closed after 166 days. New replies are no longer allowed.