SyncKit icon indicating copy to clipboard operation
SyncKit copied to clipboard

How to sync data in widget?

Open Mr-ZNN opened this issue 3 years ago • 5 comments

My project has added widget. Without the need to open the app, the data needs to be updated synchronously when the widget is updated, so I created a synchronizer in both the app and the widget, or created a synchronizer in a singleton class to give app and widget use. However, after the widget is synchronized, opening the app and synchronizing the realm in the app will report the following error: *** Terminating app due to uncaught exception 'RLMException', reason: 'Attempting to create an object of type 'GroupMessage' with an existing primary key value 'D2CB7AB4-D505-4568-83D1-CBA8EB28408B'.'

Mr-ZNN avatar Mar 02 '22 15:03 Mr-ZNN

did you figure it out? this is worrisome

aehlke avatar Aug 16 '22 18:08 aehlke

Hey, would need more information to possibly look into this. Things like snippets, a description of your flow, a description of how many Realms you're using and where you're storing... Generally it's a bad idea to create more than one synchronizer in your app for the same Realm store –still, not sure if that's what you're even doing.

mentrena avatar Aug 28 '22 10:08 mentrena

I also just ran into this with a UUID PK. I'm looking into it.

aehlke avatar Sep 02 '22 20:09 aehlke

First off I see that IceCream treats insertions and modifications to the realm db the same: https://github.com/caiyue1993/IceCream/blob/0b6b24a4eddc331361f3123eab48ef116b123161/IceCream/Classes/SyncObject.swift#L136-L149 whereas SyncKit treats them specially. I think this may be a mistake and source of a race condition or similar issue

aehlke avatar Sep 02 '22 20:09 aehlke

I've fixed it with defensive coding in my fork, BigSyncKit: https://github.com/lake-of-fire/BigSyncKit/commit/c2d9e1859fe1d3df65c7a41dda76e026cb4a3128

I collapsed .new and .changed into .newOrChanged and then allowed the realm.add to update an existing object at the site of the crash.

aehlke avatar Sep 02 '22 20:09 aehlke