Backing up and restoring local realm file

Hello! TIA for any help. Been searching online for a couple days and can’t seem to figure this out. Note, I’ve only been at my app development for about 6 months in SwiftUI, so I’m still pretty new.

Building an iOS app with Realm, currently using local-only database. Plans to develop online sync database later.

I’m building a backup and restore database option within the app that allows me to choose where to save the backup file to, and which file to restore from.

While running the app in Simulator, I’m able to back up the current database and restore - but the info within the app doesn’t reflect the restored database unless I close and reopen the app.

I’m trying to figure out how I can re-initialize the database or what I need to do, so that it recognizes the restored database info without having to restart.

This is the code for restoring:

static func restoreRealm(from backupURL: URL) {
        guard let realmURL = Realm.Configuration.defaultConfiguration.fileURL else { return }
        print("realmURL path: \(realmURL.path)")
        print("Renaming to OLDdefault.realm...")

        let oldRealmURL = realmURL.deletingLastPathComponent().appendingPathComponent("OLDdefault.realm")
        do {
            // Rename the current realm file to OLDdefault.realm if it exists
            if FileManager.default.fileExists(atPath: realmURL.path) {
                print("realmURL exists at \(realmURL.path)")
                // If OLDdefault.realm already exists, remove it before renaming the current file
                if FileManager.default.fileExists(atPath: oldRealmURL.path) {
                    print("oldRealmURL found. Removing...")
                    try FileManager.default.removeItem(at: oldRealmURL)
                try FileManager.default.moveItem(at: realmURL, to: oldRealmURL)
            print("Trying to copy file from backupURL to realmURL...")
            // Move the backup realm file to the default realm file path
            try FileManager.default.copyItem(at: backupURL, to: realmURL)
            print("Restore successful!")


        } catch {
            print("Error restoring Realm from backup: \(error)")


Any help is appreciated, tyvm