drewmccormack / ensembles

A synchronization framework for Core Data.
MIT License
1.63k stars 132 forks source link

Possible data loss when connecting a new device to ensembles #203

Closed lrrrs closed 9 years ago

lrrrs commented 9 years ago

I use Ensembles with the iCloud file system in my app Tyme which is available for iOS & OSX. And every once in a while a customer reports data loss when initially connecting his iPhone to iCloud after he already worked with the Mac app.

A few database entries are created on the Mac and synced. Then the iPhone connects and syncs. After the sync there are only entries left which were created on the iPhone. All other entries seem to be deleted.

I did not implement the "didFailToSaveMergedChangesInManagedObjectContext" delegate and try to repair or delete corrupt entries. So I'm wondering where the data loss occurs.

Did anyone experience this also? I could never reproduce the issue ...

Cheers, Lars

roostr commented 9 years ago

Is it possible that you have an autoincrementing integer unique ID? Perhaps you're creating items with ID 1, 2 and 3 on the iPad, then creating items with ID 1, 2 and 3 on the iPhone, and when you sync, the iPhone items are overwriting the iPad items.

If that's the issue, I solved it by using UUIDs for the ID column.

lrrrs commented 9 years ago

All entities have a uid property that's set once in awakeFromInsert: [[NSProcessInfo processInfo] globallyUniqueString];

And I use the persistentStoreEnsemble:globalIdentifiersForManagedObjects: delegate. So every ID should be unique. What do you mean by autoincrement ID, in the SQL database itself?

dave256 commented 9 years ago

I vaguely remembered reading something in the past about awakFromInsert could be called multiple times so I googled it. Could this cause your problem?

http://stackoverflow.com/questions/19856122/why-is-awakefrominsert-called-twice

lrrrs commented 9 years ago

Thanks! If the UID's would be not unique (set again), it would double the entries not delete them right? I just got a log from a user and all globalidentifier's seem to be ok, but he has three baselines. My guess is that somehow a new baseline is created that overwrites the existing one? Can that happen?

lrrrs commented 9 years ago

Dug deeper, but couldn't reproduce the issue here in any case. So thanks for the ideas. Will add some checks / logs and see if that happens again.