SwiftUI how to retry @AsyncOpen when internet is restored ResolvedFailed error when offline

How do I sync the realm again once internet connection is restored?

Scenario:

  • User opens the app in the subway with no internet
  • “ResolvedFailed” error occurs
  • User exists the subway and internet is restored

I know the user can hard-close the app and re-open, but it’s bad user experience. Is there a better way?

Here’s a code sample:

@main
struct MyApp: SwiftUI.App {
    let app: RealmSwift.App? = RealmSwift.App(id: YOUR_APP_SERVICES_APP_ID_HERE)
    
    var body: some Scene {
        WindowGroup {
		
            Group {
                if let app = app, let user = app.currentUser, let config = user.flexibleSyncConfiguration(initialSubscriptions: { subs in INITIAL_SUBSCRIPTION_WORK}, rerunOnOpen: true) {
                    OpenSyncedRealmView()
                        .environment(\.realmConfiguration, config)
                } else {
                    AuthView()
                }
            }
        }
    }
}

struct OpenSyncedRealmView: View {
    @AsyncOpen(appId: my_app_id, timeout: 10000) var asyncOpen
    
    var body: some View {
        switch asyncOpen {
        case .error(let error):
                Text(error.localizedDescription) // This is "ResolvedFailed" when user is in subway (or on flight mode)
        case .connecting, .waitingForUser, .progress:
            LoadingView()
        case .open(let realm):
            MyAppMainView()
                .environment(\.realm, realm)
        }
    }
}
2 Likes

I have also come across this problem. Is there a solution to restart the sync?