Skip to content
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

Closed
cmelchior opened this issue May 27, 2020 · 22 comments
Closed

Opening same Realm with different partition values #6882

cmelchior opened this issue May 27, 2020 · 22 comments
Assignees
Labels
MongoDBRealm This issue is related to the MongoDB Realm Beta T-Internal

Comments

@cmelchior
Copy link
Contributor

cmelchior commented May 27, 2020

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.

@rorbech
Copy link
Contributor

rorbech commented May 27, 2020

Just hit that issue moments ago when opening two Realms with different partition keys. It throws a

java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file.
Cached configuration:
...
canonicalPath: /data/data/io.realm.test/files/mongodb-realm/5ecec9188db8581fc64c24cf/realm-sdk-integration-tests-djakl/5ecec9188db8581fc64c24cf/default.realm
...

New configuration:
...
canonicalPath: /data/data/io.realm.test/files/mongodb-realm/5ecec9188db8581fc64c24cf/realm-sdk-integration-tests-djakl/5ecec9188db8581fc64c24cf/default.realm
...

@cmelchior
Copy link
Contributor Author

cmelchior commented May 27, 2020

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.

@rorbech
Copy link
Contributor

rorbech commented May 27, 2020

Yes, seems like SyncConfiguration.equals() do not compare partitionValue. But nonetheless, the dumped canonical paths seems to be identical too.

@cmelchior
Copy link
Contributor Author

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.

@rorbech
Copy link
Contributor

rorbech commented May 28, 2020

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"!?

@cmelchior
Copy link
Contributor Author

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.

@rorbech rorbech added the MongoDBRealm This issue is related to the MongoDB Realm Beta label Jun 4, 2020
@sipersso
Copy link

sipersso commented Jun 22, 2020

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:

val privateConfiguration = SyncConfiguration.Builder(realmApp.currentUser()!!, "owner_id="+user!!.id).addModule(PrivateModule()).waitForInitialRemoteData().build()

val publicConfiguration = SyncConfiguration.Builder(realmApp.currentUser()!!, "PUBLIC").addModule(PublicModule()).waitForInitialRemoteData().build()

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?

@ianpward
Copy link

@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
https://docs.mongodb.com/realm/android/quick-start/#open-a-realm

@sipersso
Copy link

val user: User? = app.currentUser()
val partitionValue: String = "myPartition"
val config = SyncConfiguration.Builder(user!!, partitionValue)
             .waitForInitialRemoteData()
             .build()

var realm: Realm
// Sync all realm changes via a new instance, and when that instance has been successfully created connect it to an on-screen list (a recycler view)
Realm.getInstanceAsync(config, object: Realm.Callback() {
   override fun onSuccess(_realm: Realm) {
         // since this realm should live exactly as long as this activity, assign the realm to a member variable
         realm = _realm
   }
})

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)

@ianpward
Copy link

You need to use two separate realm variable names and call separate invocations of getInstanceAsync which return different realm references. As in:

Realm.getInstanceAsync(publicConfiguration, object : Realm.Callback() { override fun onSuccess(publicRealm: Realm) {

and

Realm.getInstanceAsync(privateConfiguration, object : Realm.Callback() { override fun onSuccess(privateRealm: Realm) {

@sipersso
Copy link

sipersso commented Jun 23, 2020

You need to use two separate realm variable names and call separate invocations of getInstanceAsync which return different realm references. As in:

Realm.getInstanceAsync(publicConfiguration, object : Realm.Callback() { override fun onSuccess(publicRealm: Realm) {

and

Realm.getInstanceAsync(privateConfiguration, object : Realm.Callback() { override fun onSuccess(privateRealm: Realm) {

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

val publicRealm = Realm.getInstance(publicConfiguration)
val privateRealm = Realm.getInstance( privateConfiguraton)

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."

@ianpward
Copy link

@sipersso I think this is because you are using the setDefaultConfiguration API - don't use that and instead specify separate configurations

@sipersso
Copy link

@ianpward none of these configurations are set as default. I do have a default configuration, but it is separate from these two.

@ianpward
Copy link

@sipersso That's your issue - please remove the default configuration. It's designed for single realm apps - not multi-realm

@sipersso
Copy link

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 ;)

@sipersso
Copy link

@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"

@ianpward
Copy link

Can you share the full code please?

@sipersso
Copy link

I can't share my code, but I'll see if I can provide a small sample to reproduce it.

@sipersso
Copy link

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?

@cmelchior
Copy link
Contributor Author

@sipersso @ianpward

Yes, sorry for not noticing this discussion sooner, but you are running into the exact problem this issue is about:

val user = realmApp.currentUser()!!

val privateConfiguration = SyncConfiguration.Builder(user, "owner_id="+user!!.id)
  .addModule(PrivateModule())
  .waitForInitialRemoteData()
  .build()

val publicConfiguration = SyncConfiguration.Builder(user, "PUBLIC")
  .addModule(PublicModule())
  .waitForInitialRemoteData()
  .build()

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 <rootDir>/<appId>/<userId>/default.realm while it should be <rootDir>/<appId>/<userId>/<hashedPartionKey>/default.realm.

Sorry for the inconvenience. It is something that is on our radar and we are looking into fixing it quite soon.

@sipersso
Copy link

Thanks! I’ll just wait for the fix then ;)

@RealmBot RealmBot changed the title Test opening same Realm with different partition values Opening same Realm with different partition values Jun 24, 2020
@cmelchior
Copy link
Contributor Author

Fixed in #6973

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
MongoDBRealm This issue is related to the MongoDB Realm Beta T-Internal
Projects
None yet
Development

No branches or pull requests

4 participants