Realm backed by several files?

66 views Asked by At

Is it possible to manage data stored amongst several files?

Let's say I have several files data1.realm, data2.realm, data3.realm, etc. containing objects with the same model. Is it possible to get a unique RLMRealm instance that will access the datas of all these files?

If not, what is the best way to handle this situation? Migration?

1

There are 1 answers

0
TiM On

It's definitely possible to manage data stored amongst separate Realms, but it wouldn't be automatic. You would need to manage access to this data in your own app's logic.

RLMRealm instances themselves represent a single file on disk and cannot be dynamically created to reference combinations of other Realms. Once an RLMObject has been added to a parent RLMRealm, it cannot be moved/backed to another RLMRealm representing a different file.

It most likely depends on your specific use-cases, but the simplest solution would be to simply query for your objects in separate RLMRealm instances for each file, and placing the resulting RLMResults objects from each one in an NSArray.

While data can't be directly shared between Realms, you could use globally unique primary keys (For example NSUUID) to indicate relationships between objects in different Realms.

If you need, it's also possible to create Realmless copies of RLMObjects if you do end up wanting to move objects between Realms:

Dog *savedDog = [[Dog allObjects] firstObject];
Dog *copiedDog = [[Dog alloc] initWithValue:savedDog];