Skip to content

Commit e8949fe

Browse files
authored
[RKOTLIN-1079] Add support for transfer progress estimate (#1575)
1 parent 3255d12 commit e8949fe

File tree

10 files changed

+416
-123
lines changed

10 files changed

+416
-123
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ This release will bump the Realm file format 24. Opening a file with an older fo
1111
* Some authentication related operations will no longer throw specialized `InvalidCredentialsException` and `CredentialsCannotBeLinkedException` but the more general `AuthException` and `ServiceException`. (Issue [#1763](https://github.com/realm/realm-kotlin/issues/1763)/[RKOTLIN-1091](https://jira.mongodb.org/browse/RKOTLIN-1091))
1212
* [Sync] Removed deprecated methods `User.identity` and `User.provider`, user identities can be accessed with the already existing `User.identities`. (Issue [#1751](https://github.com/realm/realm-kotlin/issues/1751) [JIRA](https://jira.mongodb.org/browse/RKOTLIN-1083))
1313
* [Sync] `App.allUsers` does no longer return a map, but only a list of users known locally. (Issue [#1751](https://github.com/realm/realm-kotlin/issues/1751) [JIRA](https://jira.mongodb.org/browse/RKOTLIN-1083))
14-
* [Sync ]Removed deprecated `DiscardUnsyncedChangesStrategy.onError`. (Issue [#1755](https://github.com/realm/realm-kotlin/issues/1755) [JIRA](https://jira.mongodb.org/browse/RKOTLIN-1085))
14+
* [Sync] Removed deprecated `DiscardUnsyncedChangesStrategy.onError`. (Issue [#1755](https://github.com/realm/realm-kotlin/issues/1755) [JIRA](https://jira.mongodb.org/browse/RKOTLIN-1085))
15+
* [Sync] Sync progress notifications now reports an estimate ranged from `0.0` to `1.0` with `Progress.estimate` instead of `transferredBytes` and `totalBytes`. (Issue [#1744](https://github.com/realm/realm-kotlin/issues/1744) [RKOTLIN-1079](https://jira.mongodb.org/browse/RKOTLIN-1079)).
1516

1617
### Enhancements
1718
* Support for RealmLists and RealmDictionaries in `RealmAny`. (Issue [#1434](https://github.com/realm/realm-kotlin/issues/1434))
1819
* Optimized `RealmList.indexOf()` and `RealmList.contains()` using Core implementation of operations instead of iterating elements and comparing them in Kotlin. (Issue [#1625](https://github.com/realm/realm-kotlin/pull/1666) [RKOTLIN-995](https://jira.mongodb.org/browse/RKOTLIN-995)).
1920
* Add support for filtering logs by category. (Issue [#1691](https://github.com/realm/realm-kotlin/issues/1691) [JIRA](https://jira.mongodb.org/browse/RKOTLIN-1038))
2021
* [Sync] Add Mongo Client API to access Atlas App Service collections. It can be accessed through `User.mongoClient`. (Issue [#972](https://github.com/realm/realm-kotlin/issues/972)/[RKOTLIN-612](https://jira.mongodb.org/browse/RKOTLIN-612))
22+
* [Sync] Sync progress notifications is now also supported for flexible sync configurations. (Issue [#1744](https://github.com/realm/realm-kotlin/issues/1744) [RKOTLIN-1079](https://jira.mongodb.org/browse/RKOTLIN-1079)).
2123

2224
### Fixed
2325
* Inserting the same typed link to the same key in a dictionary more than once would incorrectly create multiple backlinks to the object. This did not appear to cause any crashes later, but would have affecting explicit backlink count queries (eg: `...@links.@count`) and possibly notifications (Core Issue [realm/realm-core#7676](https://github.com/realm/realm-core/issues/7676) since v1.16.0).

packages/cinterop/src/commonMain/kotlin/io/realm/kotlin/internal/interop/Callback.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fun interface AsyncOpenCallback {
8585
}
8686

8787
fun interface ProgressCallback {
88-
fun onChange(transferredBytes: Long, totalBytes: Long)
88+
fun onChange(progressEstimate: Double)
8989
}
9090

9191
fun interface ConnectionStateChangeCallback {

packages/cinterop/src/nativeDarwin/kotlin/io/realm/kotlin/internal/interop/RealmInterop.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ fun String.toRString(memScope: MemScope) = cValue<realm_string_t> {
270270
set(memScope, this@toRString)
271271
}
272272

273+
@OptIn(ExperimentalForeignApi::class)
273274
@Suppress("LargeClass", "FunctionNaming")
274275
actual object RealmInterop {
275276

@@ -2813,10 +2814,9 @@ actual object RealmInterop {
28132814
return CPointerWrapper(
28142815
realm_wrapper.realm_sync_session_register_progress_notifier(
28152816
syncSession.cptr(),
2816-
staticCFunction<COpaquePointer?, ULong, ULong, Double, Unit> { userData, transferred_bytes, total_bytes, _ ->
2817+
staticCFunction<COpaquePointer?, ULong, ULong, Double, Unit> { userData, _, _, progress_estimate ->
28172818
safeUserData<ProgressCallback>(userData).run {
2818-
// TODO Progress ignored until https://github.com/realm/realm-kotlin/pull/1575
2819-
onChange(transferred_bytes.toLong(), total_bytes.toLong())
2819+
onChange(progress_estimate)
28202820
}
28212821
},
28222822
direction.nativeValue,

packages/jni-swig-stub/src/main/jni/realm_api_helpers.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,14 +1244,13 @@ sync_after_client_reset_handler(realm_sync_config_t* config, jobject after_handl
12441244
}
12451245

12461246
void
1247-
realm_sync_session_progress_notifier_callback(void *userdata, uint64_t transferred_bytes, uint64_t total_bytes, double progress) {
1247+
realm_sync_session_progress_notifier_callback(void *userdata, uint64_t, uint64_t, double progress_estimate) {
12481248
auto env = get_env(true);
12491249

1250-
// TODO Progress ignored until https://github.com/realm/realm-kotlin/pull/1575
1251-
static JavaMethod java_callback_method(env, JavaClassGlobalDef::progress_callback(), "onChange", "(JJ)V");
1250+
static JavaMethod java_callback_method(env, JavaClassGlobalDef::progress_callback(), "onChange", "(D)V");
12521251

12531252
jni_check_exception(env);
1254-
env->CallVoidMethod(static_cast<jobject>(userdata), java_callback_method, jlong(transferred_bytes), jlong(total_bytes));
1253+
env->CallVoidMethod(static_cast<jobject>(userdata), java_callback_method, jdouble(progress_estimate));
12551254
jni_check_exception(env);
12561255
}
12571256

packages/jni-swig-stub/src/main/jni/realm_api_helpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void
107107
sync_after_client_reset_handler(realm_sync_config_t* config, jobject after_handler);
108108

109109
void
110-
realm_sync_session_progress_notifier_callback(void *userdata, uint64_t transferred_bytes, uint64_t total_bytes, double progress);
110+
realm_sync_session_progress_notifier_callback(void *userdata, uint64_t, uint64_t, double progress_estimate);
111111

112112
void
113113
realm_sync_session_connection_state_change_callback(void *userdata, realm_sync_connection_state_e old_state, realm_sync_connection_state_e new_state);

packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/internal/SyncSessionImpl.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.realm.kotlin.mongodb.internal
1818

19-
import io.realm.kotlin.internal.InternalConfiguration
2019
import io.realm.kotlin.internal.NotificationToken
2120
import io.realm.kotlin.internal.RealmImpl
2221
import io.realm.kotlin.internal.interop.CoreError
@@ -109,9 +108,6 @@ internal open class SyncSessionImpl(
109108
direction: Direction,
110109
progressMode: ProgressMode,
111110
): Flow<Progress> {
112-
if ((configuration as InternalConfiguration).isFlexibleSyncConfiguration) {
113-
throw UnsupportedOperationException("Progress listeners are not supported for Flexible Sync.")
114-
}
115111
return realm.scopedFlow {
116112
callbackFlow {
117113
val token: AtomicRef<Cancellable> =
@@ -124,8 +120,8 @@ internal open class SyncSessionImpl(
124120
Direction.UPLOAD -> ProgressDirection.RLM_SYNC_PROGRESS_DIRECTION_UPLOAD
125121
},
126122
progressMode == ProgressMode.INDEFINITELY
127-
) { transferredBytes: Long, totalBytes: Long ->
128-
val progress = Progress(transferredBytes.toULong(), totalBytes.toULong())
123+
) { progressEstimate: Double ->
124+
val progress = Progress(progressEstimate)
129125
trySendWithBufferOverflowCheck(progress)
130126
if (progressMode == ProgressMode.CURRENT_CHANGES && progress.isTransferComplete) {
131127
close()

packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/sync/Progress.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,9 @@ package io.realm.kotlin.mongodb.sync
2121
*/
2222
public data class Progress(
2323
/**
24-
* Total number of bytes that has been transferred by the [SyncSession].
24+
* Transfer progress estimation ranged from 0.0 to 1.0.
2525
*/
26-
val transferredBytes: ULong,
27-
/**
28-
* Total number of transferable bytes (bytes that have been transferred + pending bytes not
29-
* yet transferred).
30-
*/
31-
val transferableBytes: ULong
26+
val estimate: Double,
3227
) {
3328
/**
3429
* Property indicating if all pending bytes have been transferred.
@@ -40,5 +35,5 @@ public data class Progress(
4035
* flow can continue to emit events with `isTransferComplete = false` for subsequent events
4136
* after returning a progress indicator with `isTransferComplete = true`.
4237
*/
43-
public val isTransferComplete: Boolean = transferredBytes >= transferableBytes
38+
public val isTransferComplete: Boolean = estimate >= 1.0
4439
}

packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/sync/SyncSession.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ public interface SyncSession {
136136
*
137137
* The flow has an internal buffer of [Channel.BUFFERED] but if the consumer fails to consume the
138138
* elements in a timely manner the flow will be completed with an [IllegalStateException].
139-
*
140-
* @throws UnsupportedOperationException if invoked on a realm with Flexible Sync enabled.
141139
*/
142140
public fun progressAsFlow(
143141
direction: Direction,

0 commit comments

Comments
 (0)