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

后端 未结 1 1390
死守一世寂寞
死守一世寂寞 2020-12-07 23:57

I have a problem with an iCloud shoebox application and hope some-one can help me (I\'ve spent many hours fighting it in vain).

The App: - A simple

相关标签:
1条回答
  • 2020-12-08 00:12

    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
    0 讨论(0)
提交回复
热议问题