commonMain.io.realm.kotlin.mongodb.sync.SyncSession.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of library-sync Show documentation
Show all versions of library-sync Show documentation
Sync Library code for Realm Kotlin. This artifact is not supposed to be consumed directly, but through 'io.realm.kotlin:gradle-plugin:1.6.2' instead.
/*
* Copyright 2021 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.realm.kotlin.mongodb.sync
import io.realm.kotlin.Realm
import io.realm.kotlin.mongodb.User
import io.realm.kotlin.mongodb.exceptions.SyncException
import io.realm.kotlin.mongodb.sync.SyncSession.ErrorHandler
import io.realm.kotlin.mongodb.sync.SyncSession.State.ACTIVE
import io.realm.kotlin.mongodb.sync.SyncSession.State.DYING
import kotlinx.coroutines.flow.Flow
import kotlin.time.Duration
/**
* A session controls how data is synchronized between a single Realm on the device and MongoDB on
* the server.
*
* A `SyncSession` is created by opening a Realm instance using a [SyncConfiguration]. Once a
* session has been created, it will continue to exist until the app is closed or the [Realm] is
* closed.
*
* A session is controlled by Realm, but can provide additional information in case of errors.
* These errors are passed along in the [ErrorHandler].
*
* When creating a session, Realm will establish a connection to the server. This connection is
* controlled by Realm and might be shared between multiple sessions.
*
* The session itself has a different lifecycle than the underlying connection.
*
* The [SyncSession] object is thread safe.
*/
public interface SyncSession {
/**
* The [SyncConfiguration] responsible for controlling the session.
*
* @throws IllegalStateException if accessed from inside a [SyncSession.ErrorHandler] due to session errors.
*/
public val configuration: SyncConfiguration
/**
* The [User] used to authenticate the session on Atlas App Services.
*/
public val user: User
/**
* The current session state. See [State] for more details about each state.
*/
public val state: State
/**
* The current [ConnectionState].
*/
public val connectionState: ConnectionState
/**
* Calling this method will block until all known remote changes have been downloaded and
* applied to the Realm or the specified timeout is hit. This will involve network access, so
* calling this method should only be done from a non-UI thread.
*
* @param timeout Maximum amount of time before this method should return.
* @return `true` if the data was downloaded. `false` if the download timed out before it
* could complete. The download will continue in the background, even after returning `false`.
* @throws IllegalArgumentException if `timeout` is <= 0.
* @throws IllegalStateException if called from inside a [SyncSession.ErrorHandler].
* @throws SyncException if a problem was encountered with the connection during the download.
*/
public suspend fun downloadAllServerChanges(timeout: Duration = Duration.INFINITE): Boolean
/**
* Calling this method will block until all known local changes have been uploaded to the server
* or the specified timeout is hit. This will involve network access, so calling this method
* should only be done from a non-UI thread.
*
* @param timeout Maximum amount of time before this method should return.
* @return `true` if the data was uploaded. `false` if the upload timed out before it
* could complete. The upload will continue in the background, even after returning `false`.
* @throws IllegalArgumentException if `timeout` is <= 0.
* @throws IllegalStateException if called from inside a [SyncSession.ErrorHandler].
* @throws SyncException if a problem was encountered with the connection during the upload.
*/
public suspend fun uploadAllLocalChanges(timeout: Duration = Duration.INFINITE): Boolean
/**
* Pauses synchronization with Atlas until the Realm is closed and re-opened again.
*
* Synchronization can also be re-activated by calling [resume].
*
* If the session is already [State.INACTIVE], calling this method will do nothing.
*/
public fun pause()
/**
* Attempts to resume the session and activate synchronization with Atlas.
*
* This happens automatically when opening the Realm, so doing it manually should only
* be needed if the session was paused using [pause].
*
* If the session was already [State.ACTIVE], calling this method will do nothing.
*
* If the session state is [State.DYING], the session will be moved back to [State.ACTIVE].
*/
public fun resume()
/**
* Create a [Flow] of [Progress]-events that track either downloads or uploads done by the [SyncSession].
*
* This is only an indicator of transferring of data and an [Progress]-event with
* [Progress.isTransferComplete] being `true` does not guarantee that the data is already
* visible in the realm. To wait for data being integrated and visible use
* [downloadAllServerChanges]/[uploadAllLocalChanges].
*
* If the flow is created with [ProgressMode.CURRENT_CHANGES] the [Progress] will
* only ever increase and will complete once `Progress.isTransferComplete = true`.
*
* If the flow is created with [ProgressMode.INDEFINITELY] the [Progress] can both
* increase and decrease since more changes might be added while the flow is still active. This
* means that it is possible for one [Progress] instance to report
* `isTransferComplete = true` and subsequent instances to report `isTransferComplete = false`.
*
* The flow will be completed if the realm is closed.
*
* The flow has an internal buffer of [Channel.BUFFERED] but if the consumer fails to consume the
* elements in a timely manner the flow will be completed with an [IllegalStateException].
*
* @throws UnsupportedOperationException if invoked on a realm with Flexible Sync enabled.
*/
public fun progressAsFlow(
direction: Direction,
progressMode: ProgressMode,
): Flow
© 2015 - 2025 Weber Informatics LLC | Privacy Policy