-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Opening same Realm with different partition values #6882
Comments
Just hit that issue moments ago when opening two Realms with different partition keys. It throws a
|
Most likely this is this issue: https://stackoverflow.com/questions/36907001/open-realm-with-new-realmconfiguration ? If not, it does indicate opening with different partition values doesn't work right now. |
Yes, seems like |
Yes, I'm pretty sure we don't support it right now (which is something we need to fix). Doubt we can make it before Live though. |
I added a test for it inAdded a test for it in #6884 (https://github.com/realm/realm-java/pull/6884/files#diff-4310bb8d195ff26f79c242ae84e10852R191). Or at least for the case of the title. The actual description refers to "different Realms but same partition value"!? |
My mistake. The title is correct. |
What is the recommended way to do this in the meantime? Add a custom location for each partition? I have a private partition for userData and one public for read only data from the server. I thought I'd be able to create create a sync configuration for each of these like this:
But it seems like both these realms are stored in default.realm and opening both will lead to a crash with the error message that the realm cannot be opened with a different configuration, just as mentioned in this issue. I am not supposed to have to specify a different location for each partition right? EDIT: It doesn 't seem to be possible to specify the location for in the sync configuration builder. Does that mean it is currently not possible to open two synced realm with different partition names? |
@sipersso How are you opening the realms? You should be naming them different variable names for different calls of the getInstanceAsync method, ie. myPrivateRealm and myPublicRealm |
If I look at this part of the documentation.. I don't see any other difference in naming except for the partition name in the configuration. As you can see in my example I have the partitionname PUBLIC realm configuration and "owner_id=xx" for the private configuration. I am not sure what you mean by naming "them different variables". I do have one private configuration and one public configuration. Opening a realm using either one of the configurations is fine, but if I open one realm using one configuration and then try to open another, the app crashes. Not sure if it can have anything to do with caching? I have both realms open at the same time (to be able to copy data from one to another) |
You need to use two separate realm variable names and call separate invocations of getInstanceAsync which return different realm references. As in:
and
|
Of course I don't store the realms in the same variable. I don't use getInstanceAsync in this case, but I have opened the realms using that method earlier. Do I always have to use getInstanceAsync (which requires a network connection)? The code I use in this case to open the realms is this
Both of these will point to the default.realm file. The first call is ok, the second one crashes with the error "java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file." |
@sipersso I think this is because you are using the setDefaultConfiguration API - don't use that and instead specify separate configurations |
@ianpward none of these configurations are set as default. I do have a default configuration, but it is separate from these two. |
@sipersso That's your issue - please remove the default configuration. It's designed for single realm apps - not multi-realm |
Ok, I see, thanks. Quite a bit of refactoring then to provide a smooth transition. I have only been using a local realm before (default) and wanted to add separate synced realms. But that would explain it. The error message could be improved though ;) |
@ianpward I just tried removing the default configuration for my local realm. It doesn't help. I still get the same error message when opening the second synced realm. Opening the local realm is fine. Opening one or the other of the synced realms is also fine. But when opening both synced realms, the app crashes with the "java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file" |
Can you share the full code please? |
I can't share my code, but I'll see if I can provide a small sample to reproduce it. |
However if I read the description correctly I can see a likely reason why it doesn’t work. Both configurations seem to use the same default.realm file even though the partition is different. So I am in fact opening the same Realm file using two different configurations. The issue is that they shouldn’t be the same file right? |
Yes, sorry for not noticing this discussion sooner, but you are running into the exact problem this issue is about:
Both point to the same server-side Realm as the configuration is using the same user. This means that currently, they point to the same local path on the device. This is wrong since you have two different partition keys defined. it should result in two different paths locally. Basically right now it translates to something like Sorry for the inconvenience. It is something that is on our radar and we are looking into fixing it quite soon. |
Thanks! I’ll just wait for the fix then ;) |
Fixed in #6973 |
We need to ensure that if the same user opens the same Realm with the same partition value, that it will result in different Realms on disk.
Right now I don't believe this work correctly, but as a minimum, we need to have a test for it.
The text was updated successfully, but these errors were encountered: