realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.34k stars 2.15k forks source link

Crash in migration process #7807

Closed santieduardo closed 2 years ago

santieduardo commented 2 years ago

How frequently does the bug occur?

All the time

Description

I am trying to migrate a "table" that the id column is not a Primary Key and it has some duplicate registers.

So, I have created a migration to add id column as Primary Key and delete the duplicate registers. But I have a crash when I try to delete de oldObject.

Here my code:

        var idsSet = Set<String>()

        migration.enumerateObjects(ofType: RopeWorkoutItemObject.className()) { oldObject, newObject in
            guard
                let unwrapOldObject = oldObject,
                let id = unwrapOldObject["id"] as? String else { return }

            Logger.warning("Realm Migration to schemaVersion 13")

            if !idsSet.contains(id) {
                newObject?["id"] = id
                idsSet.insert(id)
                Logger.debug("migrated \(id)")
            } else {
                migration.delete(oldObject!)
                Logger.custom("deleted \(id)", prepended: "")
            }
        }

Stacktrace & log output

2022-05-20 15:39:04.166999+0100 ProzisGo[11828:522715] *** Terminating app due to uncaught exception 'RLMException', reason: 'Can only delete an object from the Realm it belongs to.'
*** First throw call stack:
(0x18102fd0c 0x198820ee4 0x10975a85c 0x109920160 0x10970aa84 0x1080c9778 0x1026c0228 0x1080c9374 0x1080c93e0 0x1080c9470 0x10970a0dc 0x1080c9184 0x1026bfcfc 0x102ade284 0x1081239f4 0x108123bdc 0x1080c9d40 0x108123c10 0x108123c64 0x108123cc8 0x10970a704 0x109930dbc 0x109930bc0 0x109930b18 0x109930a58 0x10992ee74 0x109ccb660 0x109c6a6c4 0x109cc81d0 0x10991cab0 0x1080800c4 0x108108518 0x10214a61c 0x102383db4 0x102383a98 0x10204e068 0x10204de7c 0x10225c160 0x10225c0e8 0x10225c494 0x107559fc8 0x10755b934 0x10225c510 0x1028479d8 0x1028488ac 0x102848c5c 0x1835df16c 0x1837b3740 0x18379ce28 0x18360ad24 0x183471cf0 0x1835945ec 0x1836a9994 0x1838d1dd0 0x183861b38 0x183472a3c 0x18352e50c 0x1834747cc 0x183558d44 0x1839ae9c0 0x1834a2474 0x1834e40f4 0x18360f78c 0x183557160 0x19218fc50 0x1921b1f98 0x192172bdc 0x192173fd4 0x107559fc8 0x10755d4cc 0x1921742ac 0x1921737c0 0x192177960 0x1810504ec 0x18106061c 0x180fa2824 0x180fa7ef8 0x180fbb240 0x1a1abd988 0x1837bb41c 0x183554b88 0x10202a580 0x105ed03d0)
libc++abi: terminating with uncaught exception of type NSException
2022-05-20 15:39:04.167362+0100 ProzisGo[11828:522941] [connection] nw_resolver_start_query_timer_block_invoke [C2] Query fired: did not receive all answers in time for api.mixpanel.com:443
dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib
*** Terminating app due to uncaught exception 'RLMException', reason: 'Can only delete an object from the Realm it belongs to.'
terminating with uncaught exception of type NSException

Can you reproduce the bug?

Yes, always

Reproduction Steps

No response

Version

10.15.1

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

iOS 14, 15

Build environment

Xcode version: 13.4 (13F17a) Dependency manager and version: CocoaPods 1.11.3

ejm01 commented 2 years ago

oldObject are objects from the old realm and are read only. newObject are objects from the new realm and are read/write. Ultimately the objects you want to delete will be in newObject. You may need to split this logic into two migration.enumerateObject blocks. You'd delete any newObjects in the second block.

See here and here for more info and examples.

santieduardo commented 2 years ago

Thank you very much for your help!