mentrena / SyncKit

Automatic CloudKit synchronization
https://mentrena.github.io/SyncKit/
MIT License
507 stars 59 forks source link

App crash with ‘RLMException’, reason: ’Attempting to create an object of type ‘MyType’ with an existing primary key value ‘some_pk_value’.' #145

Open StKotok opened 3 years ago

StKotok commented 3 years ago

Hello, It seems like Realm crashes the SyncKit.

Realm documentation says that it is possible to add a flag .all to realm.add(), so that he would not crash but overwrite existing data.

What do you think about adding an update .all flag to realm.add() or raise an exception for handling it on the app side?

Thanks!

Here are the logs:

“CloudKitSynchronizer >> Initiating synchronization”
“QSCloudKitSynchronizer >> Downloaded 1 changed records >> from zone <CKRecordZoneID: 0x281357720; ownerName=__defaultOwner__, zoneName=QSCloudKitCustomZoneName>”
“QSCloudKitSynchronizer >> Downloaded 0 deleted record IDs >> from zone <CKRecordZoneID: 0x281357720; ownerName=__defaultOwner__, zoneName=QSCloudKitCustomZoneName>”
2021-03-05 13:38:45.877080+0200 DrinkWater[3753:1095927] *** Terminating app due to uncaught exception ‘RLMException’, reason: ’Attempting to create an object of type ‘DrinkEvent’ with an existing primary key value ‘636637079.097727’.'
*** First throw call stack:
(0x18636bae4 0x19a1c2cb4 0x10328b038 0x103348684 0x105162174 0x105552214 0x10555bd2c 0x105547388 0x105530ca8 0x105530d00 0x10581d5f4 0x10582c978 0x10581d5f4 0x10582ba64 0x1862eb708 0x1862e5bb0 0x1862e4cc8 0x19cad0598 0x188bbb488 0x188bc0a08 0x1008e8150 0x185fc4588)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception ‘RLMException’, reason: ’Attempting to create an object of type ‘DrinkEvent’ with an existing primary key value ‘636637079.097727’.'
terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception ‘RLMException’, reason: ’Attempting to create an object of type ‘DrinkEvent’ with an existing primary key value ‘636637079.097727’.'
terminating with uncaught exception of type NSException
mentrena commented 3 years ago

Hi @StKotok , I would need more details to be able to help.

StKotok commented 3 years ago

Hi @mentrena,

Can you reproduce the issue with the SyncKit example project?

I haven't tried it because my case isn't usual.

Can you provide code and steps needed to reproduce?

I reproduce this crash during synchronization data from the Apple Watch by both channels simultaneously: by iCloud and Bluetooth. During data synchronization with iCloud (via SyncKit), the same data piece (with the same personal key) received from the Watch via Bluetooth. It leads to the crash.

What version of SyncKit are you using?

0.9.0

mentrena commented 3 years ago

I'm afraid that I would need to be able to reproduce the issue in order to come up with a solution. There is already a test to explicitly check that it's ok to create an object with the same identifier on two devices, so the crash is happening due to something more specific. I'm happy to have a look if you can upload a project to Github, ideally with the bare minimum code that reproduces the crash.

aehlke commented 1 year ago

FWIW I've just reproduced this issue. I'll update on any findings

Ok, in my case it happens because I have a second process unrelated to syncing which also creates records locally that may already exist in iCloud. So SyncedEntity doesn't exist, gets created, and then fails when it tries to create the realm object with the data because it already exists. I've added merging logic to my fork.