Issue can be tracked here: realm/realm-swift#7017
NOTE: This only happens using InMemoryRealm
configuration something like Realm.Configuration(inMemoryIdentifier: "messagesRealm")
I am writing a cache protocol which I can implement later using any or multiple cache libraries (regardless of disk or memory), in this case it is Realm
Expectation was when the save function is called realm should successfully save the object in memory and when retrieved it should return it successfully or return all objects.
In memory realm is returning empty every time I insert something and try to fetch it (I am 100% sure it is inserting successfully and I will explain later why)
- Clone the git repository in
Code Sample
- Compile and run on a simulator/device (they both are giving the same results)
- In the
Message
field write anything - Press
Save
- Now press
Refresh
it should populate a list with data but it won't do it
- While reproducing the problem navigate to
Realm
->InMemoryRealmCache.swift
and setup a breakpoint at line94
. - Now run the application again and repeat step
3
to4
the application should break after pressingSave
- In the
lldb
writepo realm.objects(MessageRealmEntity.self)
and finally continue the application - Now press
Refresh
and the list will be populated No matter how many times you add and refresh it will work now 100%.
realm-bug_nlFwK7Nu.mov
This is a git repository for a dummy application to reproduce the bug:
https://github.com/Muhand/InMemoryRealm-Bug
Realm framework version: RealmSwift (10.1.4)
Realm Object Server version: ?
Xcode version: Version 12.2 (12B45b)
iOS/OSX version: iOS 14.2
Dependency manager + version: ?
- As can be seen in the project I am initializing
Realm
in every function in my wrapper this way I can avoidRealm accessed from incorrect thread
issue. - However, to avoid realm being initialized on different threads I made sure to set a
DispatchQueue
in which they all are initialized and ran in. - I have tried making my
Realm
object to be static and living somewhere else but this is not scalable and if someone in the team called the functions from a different thread it can crash the app with the errorRealm accessed from incorrect thread
- The wrapper works 100% fine if I am using disk realm instead of memory
- The wrapper is initialized only once so I don't have multiple instances of it