I
I
Ivan Vorobei2019-07-03 23:28:23
iOS
Ivan Vorobei, 2019-07-03 23:28:23

How to backup Realm database to a file in Swift?

How to save the Realm DB to a file, ideally with a custom extension?
How to replace the current database with such a backup?
I initialize the database like this:

let fileURL = FileManager.default
            .containerURL(forSecurityApplicationGroupIdentifier: self.identificator)!
            .appendingPathComponent("default.realm")
        let config = Realm.Configuration(fileURL: fileURL, schemaVersion: 2, migrationBlock: { (migration, oldSchemaVersion) in })
        let realm = try! Realm(configuration: config)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Vorobei, 2019-07-04
@ivanvorobei

If the Realm URL looks like this:

FileManager.default
            .containerURL(forSecurityApplicationGroupIdentifier: self.identificator)!
            .appendingPathComponent("default.realm")

It is not difficult to get a Data
object: Then you can write Data somewhere, add an extension to it, unload it ... here to taste.
To replace the Ream database with a backup, use:
You can save the backup to a file, add an extension and prompt the user to save the file (in iCloud Drive for example). To restore from a backup, you need to open the saved file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question