iosswiftrealmrealm-mobile-platform

Does In-Memory Realm have the copy of disk persisted Realm data in Swift?


Just trying to understand, when we try to access an object using an in-memory realm instance, does it replicate the disk persisted realm data at that moment or will it be a clean realm instance with no data in the objects.

I do understand that both the realm instances point to the same object schema but still confused.

I have both disk-persistence and in-memory realm in my project.

When I try to query the disk-persisted realm the data objects are returned if exists while on the other hand the if I perform the same query on in-memory realm it always returns 0 objects.

Any help would be appreciated.


Solution

  • The in-memory Realm will be a clean Realm instance. It does not interfere with any other realm (on-disk or elswhere). All entries will be gone, once your application is terminated.

    You can even have multiple in-memory Realms running at once, e.g. to have a clean database for each unit test class. Just make sure you use a different inMemoryIdentifier for each test.

    var config = Realm.Configuration()
    config.inMemoryIdentifier = self.name // use the test class name as identifier
    
    let realm = try Realm(configuration: config)
    return realm