Skip to content

Commit f0eca83

Browse files
authored
Automatic client reset on sync with "seamless loss" (#7585)
1 parent a9277eb commit f0eca83

30 files changed

+1073
-200
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
* [RealmApp] Reduced native memory usage when working with synchronized Realms.
2626
* [RealmApp] Make it possible to bundle synchronized Realms in apps using `Realm.writeCopyTo()` and `SyncConfiguration.Builder.assetFile()`.
2727
* The Realm Transformer and Realm Gradle Plugin now supports the Gradle Configuration Cache. (Issue [#7299](https://github.com/realm/realm-java/issues/7299))
28+
* [RealmApp] Introduced `SyncSession.DiscardUnsyncedChangesStrategy`, an alternative automatic client reset strategy that doesn't require the Realm to be closed, but discards any unsynced data from the client. This is now the default policy if not overridden.
29+
30+
### Deprecated
31+
* [RealmApp] `SyncSession.ClientResetHandler()`. Use `SyncSession.ManuallyRecoverUnsyncedChangesStrategy()` instead.
32+
* [RealmApp] `AppConfiguration.Builder.defaultClientResetHandler()`. Use `AppConfiguration.Builder.setDefaultSyncClientResetStrategy()` instead.
33+
* [RealmApp] `AppConfiguration.getDefaultClientResetHandler()`. Use `AppConfiguration.getDefaultSyncClientResetStrategy()` instead.
34+
* [RealmApp] `SyncConfiguration.Builder.clientResetHandler()`. Use `SyncConfiguration.Builder.setSyncClientResetStrategy()` instead.
35+
* [RealmApp] `SyncConfiguration.getClientResetHandler()`. Use `SyncConfiguration.getSyncClientResetStrategy()` instead.
2836

2937
### Fixed
3038
* [RealmApp] Setting `AppConfiguration.syncRootDirectory()` didn't have any effect beside creating the new folder. Realms were still placed in the default location.

realm/realm-library/src/androidTestObjectServer/kotlin/io/realm/AppConfigurationTests.kt

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import io.realm.log.RealmLog
2323
import io.realm.log.RealmLogger
2424
import io.realm.mongodb.*
2525
import io.realm.mongodb.log.obfuscator.HttpLogObfuscator
26+
import io.realm.mongodb.sync.DiscardUnsyncedChangesStrategy
27+
import io.realm.mongodb.sync.ManuallyRecoverUnsyncedChangesStrategy
2628
import io.realm.mongodb.sync.SyncSession
2729
import io.realm.rule.BlockingLooperThread
2830
import io.realm.util.assertFailsWithErrorCode
@@ -35,6 +37,7 @@ import org.junit.Test
3537
import org.junit.rules.TemporaryFolder
3638
import org.junit.runner.RunWith
3739
import java.io.File
40+
import java.lang.ClassCastException
3841
import java.net.URL
3942
import java.util.*
4043
import java.util.concurrent.TimeUnit
@@ -249,20 +252,58 @@ class AppConfigurationTests {
249252
}
250253

251254
@Test
252-
fun defaultClientResetHandler() {
255+
@Deprecated("defaultClientResetHandler deprecated in favor of defaultSyncClientResetStrategy")
256+
fun defaultClientResetHandler_throws() {
257+
val config = AppConfiguration.Builder("app-id")
258+
.build()
259+
260+
assertFailsWith<ClassCastException> {
261+
config.defaultClientResetHandler
262+
}
263+
}
264+
265+
@Test
266+
@Deprecated("defaultClientResetHandler deprecated in favor of defaultSyncClientResetStrategy")
267+
fun setDefaultClientResetHandler() {
253268
val handler = SyncSession.ClientResetHandler { _, _ -> }
269+
val config = AppConfiguration.Builder("app-id")
270+
.defaultClientResetHandler(handler)
271+
.build()
272+
assertEquals(config.defaultSyncClientResetStrategy, handler)
273+
}
274+
275+
@Test
276+
@Deprecated("defaultClientResetHandler deprecated in favor of defaultSyncClientResetStrategy")
277+
fun defaultClientResetHandler_invalidValuesThrows() {
278+
val builder = AppConfiguration.Builder("app-id")
279+
assertFailsWith<IllegalArgumentException> {
280+
builder.defaultClientResetHandler(TestHelper.getNull<SyncSession.ClientResetHandler>())
281+
}
282+
}
283+
284+
@Test
285+
fun defaultSyncClientStrategy() {
286+
val config = AppConfiguration.Builder("app-id")
287+
.build()
288+
289+
assertTrue(config.defaultSyncClientResetStrategy is DiscardUnsyncedChangesStrategy)
290+
}
291+
292+
@Test
293+
fun setDefaultSyncClientStrategy() {
294+
val handler = ManuallyRecoverUnsyncedChangesStrategy { _, _ -> }
254295

255296
val config = AppConfiguration.Builder("app-id")
256-
.defaultClientResetHandler(handler)
297+
.defaultSyncClientResetStrategy(handler)
257298
.build()
258-
assertEquals(config.defaultClientResetHandler, handler)
299+
assertEquals(config.defaultSyncClientResetStrategy, handler)
259300
}
260301

261302
@Test
262-
fun defaultClientResetHandler_invalidValuesThrows() {
303+
fun setDefaultSyncClientStrategy_invalidValuesThrows() {
263304
val builder = AppConfiguration.Builder("app-id")
264305
assertFailsWith<IllegalArgumentException> {
265-
builder.defaultClientResetHandler(TestHelper.getNull())
306+
builder.defaultSyncClientResetStrategy(TestHelper.getNull<ManuallyRecoverUnsyncedChangesStrategy>())
266307
}
267308
}
268309

realm/realm-library/src/androidTestObjectServer/kotlin/io/realm/entities/DefaultSyncSchema.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ const val defaultPartitionValue = "default"
3636
// EmbeddedTreeLeaf::class
3737
])
3838
class DefaultSyncSchema
39+
40+
@RealmModule(classes = [
41+
SyncColor::class
42+
])
43+
class ColorSyncSchema

0 commit comments

Comments
 (0)