diff --git a/CHANGELOG.md b/CHANGELOG.md index 80012b62d8..52fdece33a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * The Realm Transformer and Realm Gradle Plugin now supports the Gradle Configuration Cache. (Issue [#7299](https://github.com/realm/realm-java/issues/7299)) ### Fixed +* [RealmApp] Setting `AppConfiguration.syncRootDirectory()` didn't have any effect beside creating the new folder. Realms were still placed in the default location. * [RealmApp] Bug where progress notifiers continue to be called after the download of a synced realm is complete. (Issue [Realm Core #4919](https://github.com/realm/realm-core/issues/4919)) * [RealmApp] User being left in the logged in state when the user's refresh token expires. (Issue [Realm Core #4882](https://github.com/realm/realm-core/issues/4882), since v10) * Using "sort", "distinct", or "limit" as field name in query expression would cause an "Invalid predicate" error. (Issue [#7545](), since v10.X.X) diff --git a/dependencies.list b/dependencies.list index 3ea41ad4c8..59739f1c8a 100644 --- a/dependencies.list +++ b/dependencies.list @@ -4,7 +4,7 @@ REALM_CORE=11.6.0 # Version of MongoDB Realm used by integration tests # See https://github.com/realm/ci/packages/147854 for available versions -MONGODB_REALM_SERVER=2021-11-28 +MONGODB_REALM_SERVER=2021-12-01 # Common Android settings across projects GRADLE_BUILD_TOOLS=7.1.0-beta03 diff --git a/realm/realm-library/src/androidTestObjectServer/kotlin/io/realm/AppConfigurationTests.kt b/realm/realm-library/src/androidTestObjectServer/kotlin/io/realm/AppConfigurationTests.kt index 0f873b493d..8d1d1d89de 100644 --- a/realm/realm-library/src/androidTestObjectServer/kotlin/io/realm/AppConfigurationTests.kt +++ b/realm/realm-library/src/androidTestObjectServer/kotlin/io/realm/AppConfigurationTests.kt @@ -128,7 +128,7 @@ class AppConfigurationTests { @Test fun syncRootDirectory_default() { val config = AppConfiguration.Builder("app-id").build() - val expectedDefaultRoot = File(InstrumentationRegistry.getInstrumentation().targetContext.filesDir, "mongodb-realm") + val expectedDefaultRoot = InstrumentationRegistry.getInstrumentation().targetContext.filesDir assertEquals(expectedDefaultRoot, config.syncRootDirectory) } diff --git a/realm/realm-library/src/androidTestObjectServer/kotlin/io/realm/mongodb/sync/SyncedRealmTests.kt b/realm/realm-library/src/androidTestObjectServer/kotlin/io/realm/mongodb/sync/SyncedRealmTests.kt index 8846805dc7..ed5e182eaf 100644 --- a/realm/realm-library/src/androidTestObjectServer/kotlin/io/realm/mongodb/sync/SyncedRealmTests.kt +++ b/realm/realm-library/src/androidTestObjectServer/kotlin/io/realm/mongodb/sync/SyncedRealmTests.kt @@ -974,6 +974,31 @@ class SyncedRealmTests { } } + @Test + fun customSyncRoot() { + app.close() + Realm.init(InstrumentationRegistry.getInstrumentation().targetContext) + val defaultRoot = File(Realm.getApplicationContext()!!.filesDir, "mongodb-realm") + val root = File(Realm.getApplicationContext()!!.filesDir, "my-custom-dir") + if (defaultRoot.exists()) { assertTrue(defaultRoot.deleteRecursively()) } + if (root.exists()) { assertTrue(root.deleteRecursively()) } + + app = TestApp(builder = { it.syncRootDirectory(root) }) + assertTrue("Root folder wasn't created", root.exists()) + assertFalse("Default folder was created", defaultRoot.exists()) + + try { + val user: User = createNewUser() + val config = createDefaultConfig(user) + assertTrue("Wrong path: ${config.path}", config.path.contains("/my-custom-dir/mongodb-realm/")) + Realm.getInstance(config).close() + assertTrue(File(config.path).exists()) + assertFalse("Default folder was created", defaultRoot.exists()) + } finally { + assertTrue(root.deleteRecursively()) + } + } + private fun createDefaultConfig(user: User, partitionValue: String = defaultPartitionValue): SyncConfiguration { return SyncConfiguration.Builder(user, partitionValue) .modules(DefaultSyncSchema()) diff --git a/realm/realm-library/src/objectServer/java/io/realm/mongodb/App.java b/realm/realm-library/src/objectServer/java/io/realm/mongodb/App.java index f5928201b7..96cd2c242b 100644 --- a/realm/realm-library/src/objectServer/java/io/realm/mongodb/App.java +++ b/realm/realm-library/src/objectServer/java/io/realm/mongodb/App.java @@ -184,12 +184,12 @@ public App(AppConfiguration config) { private OsApp init(AppConfiguration config) { String userAgentBindingInfo = getBindingInfo(); String appDefinedUserAgent = getAppInfo(config); - String syncDir = getSyncBaseDirectory(); + String syncDir = getSyncBaseDirectory(config); return new OsApp(config, userAgentBindingInfo, appDefinedUserAgent, syncDir); } - private String getSyncBaseDirectory() { + private String getSyncBaseDirectory(AppConfiguration config) { Context context = Realm.getApplicationContext(); if (context == null) { throw new IllegalStateException("Call Realm.init() first."); @@ -213,7 +213,7 @@ private String getSyncBaseDirectory() { throw new IllegalStateException(e); } } else { - syncDir = context.getFilesDir().getPath(); + syncDir = config.getSyncRootDirectory().getPath(); } return syncDir; } diff --git a/realm/realm-library/src/objectServer/java/io/realm/mongodb/AppConfiguration.java b/realm/realm-library/src/objectServer/java/io/realm/mongodb/AppConfiguration.java index 09e2e02e39..eb42d9d160 100644 --- a/realm/realm-library/src/objectServer/java/io/realm/mongodb/AppConfiguration.java +++ b/realm/realm-library/src/objectServer/java/io/realm/mongodb/AppConfiguration.java @@ -415,11 +415,6 @@ public Builder(String appId) { if (context == null) { throw new IllegalStateException("Call `Realm.init(Context)` before calling this method."); } - File rootDir = new File(context.getFilesDir(), "mongodb-realm"); - if (!rootDir.exists() && !rootDir.mkdir()) { - throw new IllegalStateException("Could not create Sync root dir: " + rootDir.getAbsolutePath()); - } - syncRootDir = rootDir; } /** @@ -638,6 +633,11 @@ public Builder httpLogObfuscator(@Nullable HttpLogObfuscator httpLogObfuscator) * @return the AppConfiguration that can be used to create a {@link App}. */ public AppConfiguration build() { + // Initialize default sync root if none has been defined. + if (syncRootDir == null) { + syncRootDir = Realm.getApplicationContext().getFilesDir(); + } + return new AppConfiguration(appId, appName, appVersion,