I
I
Ivan Vorobei2019-06-30 14:56:26
iOS
Ivan Vorobei, 2019-06-30 14:56:26

How to get a file from Realm?

I initialize the Realm 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)

I want to make a backup, for this you need to collect the database into a . How to do it?

Answer the question

In order to leave comments, you need to log in

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

We make a Data object:
realmURL in my case this is the fileURL from the question.
Save with extension:
toFile method :

func toFile(fileName: String, extenshion: String) -> URL? {
        
        if let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
            
            let fileURL = directory.appendingPathComponent(fileName + "." + extenshion)
            
            do {
                try self.write(to: fileURL)
                return fileURL
            }
            catch {
                print("Error writing the file: \(error.localizedDescription)")
            }
        }
        
        return nil
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question