Docs Menu
Docs Home
/ /
Atlas Device SDKs
/ /

Handle Sync Errors with SwiftUI - Swift SDK

On this page

  • Handle Sync Errors

While developing an application that uses Device Sync, you should set an error handler. This error handler will detect and respond to any failed sync-related API calls.


See also:

For a complete example app with a working Sync error handler implementation, create a template app and check out the SwiftUI client. The error handler implementation is in the App.swift file.

For a SwiftUI-friendly implementation of a Sync error handler, create an ObservableObject with an optional @Published variable to contain a potential error. This handler uses the SyncManager to listen for errors. The SyncManager reports errors of the type SyncError, and it also reports other connection issues.

For more information, refer to the underlying Objective-C RLMSyncError.

final class ErrorHandler: ObservableObject {
@Published var error: Swift.Error?
init(app: RealmSwift.App) {
// Sync Manager listens for sync errors.
app.syncManager.errorHandler = { error, syncSession in
if let error = error as? SyncError {
/* Handle specific SyncError cases, or use a switch
* statement to handle all Sync error codes.
* In this case, ignore a .connectionFailed error and
* continue executing the app code. */
if error.code == .connectionFailed {
self.error = error
} else if let error = error as? POSIXError {
/* The error handler may also report NSError types to
* allow for error handling in a platform-idiomatic way.
* In this case, handle a connection timeout error as
* an .ETIMEDOUT error in the POSIXError domain. */
if error.code == .ETIMEDOUT {
self.error = error


For a list of common Device Sync errors and how to handle them, refer to Sync Errors in the App Services Device Sync documentation.

Initialize the error handler as a @StateObject. Inject it into the view hierarchy as an environment object. In this example, we display an .alert to the user when a Sync error occurs.

let app = App(id: flexibleSyncAppId)
struct realmSwiftUIApp: SwiftUI.App {
// Initialize the error handler
@StateObject var errorHandler = ErrorHandler(app: app)
var body: some Scene {
WindowGroup {
NextView(app: app)
// Inject the error handler as an environment object
// Display an alert to the user containing the error when a Sync error occurs
.alert(Text("Error"), isPresented: .constant(errorHandler.error != nil)) {
Button("OK", role: .cancel) { errorHandler.error = nil }
} message: {
Text(errorHandler.error?.localizedDescription ?? "")

Then, in the view where you are observing the Realm App, you can use the error handler as an @EnvironmentObject to react to Sync errors. An error that occurs here pops up an alert for the user, using the .alert set in the view above.

struct NextView: View {
@ObservedObject var app: RealmSwift.App
// Use the error handler that you injected into the environment
@EnvironmentObject var errorHandler: ErrorHandler
var body: some View {
Text("You might log users in or handle errors in this view")
← Filter Data

On this page