iCloud + CoreData - how to avoid pre-filled data duplication?

南笙酒味 提交于 2019-11-28 04:25:14

Strategy 1 with some modifications appeared to be a working solutions (with some flaws though).

Legend:

  • 1st device - started online without any content in the iCloud
  • 2nd device - started later than first and OFFLINE. Then it gets online after some items added

So here's the updated strategy:

  • All my categories have creation time-stamps

  • The categories cannot be renamed (only added or deleted - this is crucial)

  • All my items have a string categoryName field which gets its value upon item creation and updated whenever item is moved to a different category - this redundant information helps to achieve success;

On insertion of new Categories:

  • On insert from iCloud, I get pairs of categories with same name if any

  • Select newer duplicate categories (they will most probably have less items than old ones so we will have less dance in iCloud)

  • Move their items if any to older duplicate categories

  • Delete newer duplicate categories

On insertion of new Items - if the item belongs to deleted category:

  • CoreData tries to merge it and fails as there's no parent category any more (lots of errors in console). It promisses to insert it later.

  • After some short time it does merge and insert the item into storage but with NIL category

  • Here we pick our item up, find out it's parent category from categoryName and put it to the correct category

VOILA! - no duplicates & everybody happy

A couple of notes:

  1. I get a dance of items belonging to the 2nd device (those that will come with nil category to the 1st device) on both devices. After a couple of minutes everything is stabilized
  2. No items is lost though
  3. The dance happens only on the first iCloud sync of the 2nd (or any other subsequent device)
  4. If the 2nd device is started online for the first time the chance that duplicate categories case appears is about 25% only - tested on 3G connection - so dance should not affect the majority of users
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!