realm / realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.
Apache License 2.0
889 stars 52 forks source link

Use caller Realm instead of Notifier Realm when calculating KeyPaths. #1594

Closed cmelchior closed 7 months ago

cmelchior commented 7 months ago

Fixes errors like ❌ asFlow_objectBound_withKeyPath[jvm] java.lang.IllegalStateException: [RLM_ERR_WRONG_THREAD]: Realm accessed from incorrect thread., but could happen with any KeyPath.

The problem was that we were calling a function in the notifier (from an arbitrary thread), and then using the notifier Realm (outside the notifier thread). So depending on the timing, sometimes it would return a thread-confined Realm.

I refactored the code, so we now calculate the keypath array outside the notifier using the caller Realm which should always be safe.